/**
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You 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 org.apache.activeblaze;

import junit.framework.TestCase;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * Basic test for BlazeChannel
 */
public class BlazeChannelTest extends TestCase {
    public void testChannel() throws Exception {
        try {
            setGroupSize(2);
            final int COUNT = 10000;
            final AtomicInteger received = new AtomicInteger();
            String destination = "test.foo";

            BlazeChannel sender = getChannelFactory().createChannel();
            BlazeChannel receiver = getChannelFactory().createChannel();
            sender.start();
            receiver.start();
            final CountDownLatch latch = new CountDownLatch(COUNT);
            receiver.addBlazeTopicMessageListener(destination, new BlazeMessageListener() {
                public void onMessage(BlazeMessage message) {
                    message.size();
                    received.incrementAndGet();
                    latch.countDown();
                }
            });
            BlazeMessage msg = new BlazeMessage();
            msg.setText("value");
            for (int i = 0; i < COUNT; i++) {
                sender.broadcast(destination, msg);
                //Thread.sleep(100);
            }
            latch.await(10, TimeUnit.SECONDS);
            receiver.stop();
            sender.stop();
            assertEquals("Too many messages not sent ", 0, latch.getCount());
        } finally {
            resetGroupSize();
        }
    }

    public void testGroupBroadcast() throws Exception {
        try {
            final int GROUP_SIZE = 10;
            setGroupSize(GROUP_SIZE);

            String destination = "test.foo";
            final AtomicInteger count = new AtomicInteger();
            List<BlazeChannel> channels = new ArrayList<BlazeChannel>();

            for (int i = 0; i < GROUP_SIZE; i++) {
                BlazeChannel channel = getChannelFactory().createChannel();
                channel.start();
                channels.add(channel);
                channel.addBlazeTopicMessageListener(destination, new BlazeMessageListener() {
                    public void onMessage(BlazeMessage message) {
                        synchronized (count) {
                            if (count.incrementAndGet() == GROUP_SIZE) {
                                count.notifyAll();
                            }
                        }
                    }
                });
            }
            BlazeMessage msg = new BlazeMessage();
            msg.setText("hello");
            channels.get(0).broadcast(destination, msg);
            synchronized (count) {
                if (count.get() < GROUP_SIZE) {
                    count.wait(5000);
                }
            }

            assertEquals(GROUP_SIZE, count.get());
            for (BlazeChannel channel : channels) {
                channel.shutDown();
            }
        } finally {
            resetGroupSize();
        }
    }

    protected void setGroupSize(int groupSize) {
    }

    protected void resetGroupSize() {
    }

    protected BlazeChannelFactory getChannelFactory() {
        return new BlazeChannelFactory();
    }
}
