some gms tests. patch by johano; reviewed by jbellis for CASSANDRA-249

git-svn-id: https://svn.apache.org/repos/asf/incubator/cassandra/trunk@788771 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Jonathan Ellis 2009-06-26 17:22:40 +00:00
parent 68564e1549
commit b9da215e06
5 changed files with 129 additions and 4 deletions

View File

@ -57,6 +57,8 @@ public class FailureDetector implements IFailureDetector, FailureDetectorMBean
/* The time when the module was instantiated. */
private static long creationTime_;
private Gossiper gossiper;
public static IFailureDetector instance()
{
if ( failureDetector_ == null )
@ -66,7 +68,7 @@ public class FailureDetector implements IFailureDetector, FailureDetectorMBean
{
if ( failureDetector_ == null )
{
failureDetector_ = new FailureDetector();
failureDetector_ = new FailureDetector(Gossiper.instance());
}
}
finally
@ -80,8 +82,9 @@ public class FailureDetector implements IFailureDetector, FailureDetectorMBean
private Map<EndPoint, ArrivalWindow> arrivalSamples_ = new Hashtable<EndPoint, ArrivalWindow>();
private List<IFailureDetectionEventListener> fdEvntListeners_ = new ArrayList<IFailureDetectionEventListener>();
public FailureDetector()
public FailureDetector(Gossiper gossiper)
{
this.gossiper = gossiper;
creationTime_ = System.currentTimeMillis();
// Register this instance with JMX
try
@ -238,11 +241,9 @@ class ArrivalWindow
private static Logger logger_ = Logger.getLogger(ArrivalWindow.class);
private double tLast_ = 0L;
private BoundedStatsDeque arrivalIntervals_;
private int size_;
ArrivalWindow(int size)
{
size_ = size;
arrivalIntervals_ = new BoundedStatsDeque(size);
}

View File

@ -91,6 +91,7 @@ class HeartBeatState
{
return version_;
}
};
class HeartBeatStateSerializer implements ICompactSerializer<HeartBeatState>

View File

@ -0,0 +1,29 @@
package org.apache.cassandra.gms;
import static org.junit.Assert.*;
import org.junit.Test;
public class ArrivalWindowTest
{
@Test
public void test()
{
ArrivalWindow window = new ArrivalWindow(4);
//base readings
window.add(111);
window.add(222);
window.add(333);
window.add(444);
window.add(555);
//all good
assertEquals(0.4342, window.phi(666), 0.01);
//oh noes, a much higher timestamp, something went wrong!
assertEquals(9.566, window.phi(3000), 0.01);
}
}

View File

@ -0,0 +1,37 @@
package org.apache.cassandra.gms;
import static org.junit.Assert.*;
import java.io.IOException;
import org.apache.cassandra.io.DataInputBuffer;
import org.apache.cassandra.io.DataOutputBuffer;
import org.apache.cassandra.net.EndPoint;
import org.junit.Test;
public class GossipDigestTest
{
@Test
public void test() throws IOException
{
EndPoint endPoint = new EndPoint("127.0.0.1", 3333);
int generation = 0;
int maxVersion = 123;
GossipDigest expected = new GossipDigest(endPoint, generation, maxVersion);
//make sure we get the same values out
assertEquals(endPoint, expected.getEndPoint());
assertEquals(generation, expected.getGeneration());
assertEquals(maxVersion, expected.getMaxVersion());
//test the serialization and equals
DataOutputBuffer output = new DataOutputBuffer();
GossipDigest.serializer().serialize(expected, output);
DataInputBuffer input = new DataInputBuffer();
input.reset(output.getData(), output.getLength());
GossipDigest actual = GossipDigest.serializer().deserialize(input);
assertEquals(0, expected.compareTo(actual));
}
}

View File

@ -0,0 +1,57 @@
package org.apache.cassandra.utils;
import static org.junit.Assert.*;
import java.util.Iterator;
import org.junit.Test;
public class BoundedStatsDequeTest
{
@Test
public void test()
{
int size = 4;
BoundedStatsDeque bsd = new BoundedStatsDeque(size);
//check the values for an empty result
assertEquals(0, bsd.size());
assertEquals(0, bsd.sum(), 0.001d);
assertEquals(Double.NaN, bsd.mean(), 0.001d);
assertEquals(Double.NaN, bsd.variance(), 0.001d);
assertEquals(Double.NaN, bsd.stdev(), 0.001d);
assertEquals(0, bsd.sumOfDeviations(), 0.001d);
bsd.add(1d); //this one falls out, over limit
bsd.add(2d);
bsd.add(3d);
bsd.add(4d);
bsd.add(5d);
//verify that everything is in there
Iterator<Double> iter = bsd.iterator();
assertTrue(iter.hasNext());
assertEquals(2d, iter.next(), 0);
assertTrue(iter.hasNext());
assertEquals(3d, iter.next(), 0);
assertTrue(iter.hasNext());
assertEquals(4d, iter.next(), 0);
assertTrue(iter.hasNext());
assertEquals(5d, iter.next(), 0);
assertFalse(iter.hasNext());
//check results
assertEquals(size, bsd.size());
assertEquals(14, bsd.sum(), 0.001d);
assertEquals(3.5, bsd.mean(), 0.001d);
assertEquals(1.25, bsd.variance(), 0.001d);
assertEquals(1.1180d, bsd.stdev(), 0.001d);
assertEquals(5, bsd.sumOfDeviations(), 0.001d);
//check that it clears properly
bsd.clear();
assertFalse(bsd.iterator().hasNext());
}
}