package net.windwards.dnsfrontend.backend;
import junit.framework.Assert;
import net.sf.ehcache.Ehcache;
import net.windwards.dnsfrontend.util.TestUtils;
import net.windwards.dnsfrontend.util.TestingConfiguration;
import org.jgroups.JChannel;
import org.jgroups.Message;
import org.junit.After;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import java.util.Random;
import java.util.UUID;
public class TestJGroupsBackend {
private JChannel channel;
private Ehcache cache;
private JGroupsBackend backend;
@BeforeClass
public static void setup() {
TestUtils.setupLogging();
}
@Before
public void before() throws Exception {
String clusterName = "cluster-" + Long.toString(new Random().nextLong());
cache = TestUtils.getEhcache();
backend = new JGroupsBackend();
backend.setCache(cache);
backend.setConfiguration(new TestingConfiguration("bar", "example.com.", 1));
backend.setClusterName(clusterName);
backend.start();
channel = new JChannel();
channel.connect(clusterName);
}
@After
public void after() throws Exception {
channel.disconnect();
backend.stop();
}
@Test
public void basicOperation() throws Exception {
String update = "{ " +
"\"type\": \"Update\", " +
"\"name\": \"foo\", " +
"\"address\": \"192.168.0.1\", " +
"\"family\": \"ipv4\", " +
"\"moniker\": \"bar\" }";
Message message = new Message(null, null, update.getBytes());
channel.send(message);
Assert.assertEquals(0, cache.getSize());
Thread.sleep(200);
Assert.assertEquals(1, cache.getSize());
Assert.assertNotNull(cache.get("A:foo.example.com."));
Thread.sleep(1000);
Assert.assertNull(cache.get("A:foo.example.com."));
Assert.assertEquals(0, cache.getSize());
}
}