com.autsia.socialboot.SocialBootApplicationTests.java Source code

Java tutorial

Introduction

Here is the source code for com.autsia.socialboot.SocialBootApplicationTests.java

Source

/*
 *    Copyright 2015 Dmytro Titov
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *        http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */

package com.autsia.socialboot;

import com.autsia.socialboot.businesslogic.twitter.services.TwitterService;
import com.hazelcast.core.HazelcastInstance;
import com.hazelcast.core.IQueue;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.SpringApplicationConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadLocalRandom;

/**
 * Unit tests
 */
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = SocialBootApplication.class)
public class SocialBootApplicationTests {

    private static final Logger LOGGER = LoggerFactory.getLogger(SocialBootApplicationTests.class);

    public static final String text = "The quick brown fox jumps over the lazy dog";
    public static final int MEASUREMENT_DELAY = 10000;

    @Autowired
    private HazelcastInstance hazelcastInstance;

    @Autowired
    private TwitterService twitterService;

    @Test
    public void performanceTest() throws Exception {
        final IQueue<Object> queue = hazelcastInstance.getQueue(SocialBootApplication.class.getSimpleName());

        ExecutorService threadPool = Executors.newFixedThreadPool(1);
        threadPool.submit((Runnable) () -> {
            while (true) {
                String id = String.valueOf(ThreadLocalRandom.current().nextLong());
                twitterService.consumeTweetFromExternalSystem(id, text);
            }
        });
        while (queue.isEmpty()) {
            Thread.sleep(1000);
        }
        int countBefore = queue.size();
        Thread.sleep(MEASUREMENT_DELAY);
        int countAfter = queue.size();
        LOGGER.info("Tweets processed: {}", countAfter - countBefore);
    }

}