|
| 1 | +package brahms |
| 2 | + |
| 3 | +import ( |
| 4 | + "math/rand" |
| 5 | + "testing" |
| 6 | + "time" |
| 7 | + |
| 8 | + "github.com/advanderveer/go-test" |
| 9 | +) |
| 10 | + |
| 11 | +func TestBrahmsNoReply(t *testing.T) { |
| 12 | + p, _ := NewParams(0.1, 0.7, 0.2, 10, 2) |
| 13 | + r := rand.New(rand.NewSource(1)) |
| 14 | + s := NewSampler(r, p.l2()) |
| 15 | + self := NID{0x01} |
| 16 | + |
| 17 | + p0 := make(chan NID) |
| 18 | + v0 := NewView(NID{0x01}) |
| 19 | + tr0 := NewMockTransport() |
| 20 | + |
| 21 | + v1 := Brahms(self, r, p, time.Millisecond*10, s, tr0, p0, v0) |
| 22 | + |
| 23 | + //view should be unchanged transport returned nothing |
| 24 | + test.Equals(t, v0, v1) |
| 25 | + |
| 26 | + //should have pushed our own nid |
| 27 | + test.Equals(t, true, tr0.DidPush(self)) |
| 28 | + |
| 29 | + //sample should be empty |
| 30 | + test.Equals(t, View{}, s.Sample()) |
| 31 | +} |
| 32 | + |
| 33 | +func TestBrahmsWithJustPushes(t *testing.T) { |
| 34 | + p, _ := NewParams(0.1, 0.7, 0.2, 10, 2) |
| 35 | + r := rand.New(rand.NewSource(1)) |
| 36 | + s := NewSampler(r, p.l2()) |
| 37 | + self := NID{0x01} |
| 38 | + |
| 39 | + id1 := NID{0x01, 0x02} |
| 40 | + p0 := make(chan NID, 10) |
| 41 | + p0 <- id1 |
| 42 | + v0 := NewView(NID{0x01}) |
| 43 | + tr0 := NewMockTransport() |
| 44 | + |
| 45 | + // with just a pull response we update the view with just that info |
| 46 | + v1 := Brahms(self, r, p, time.Millisecond*10, s, tr0, p0, v0) |
| 47 | + test.Equals(t, 0, len(p0)) |
| 48 | + test.Equals(t, NewView(id1), v1) |
| 49 | + |
| 50 | + // but the pushed id should have been added to the sample |
| 51 | + test.Equals(t, NewView(id1), s.Sample()) |
| 52 | + |
| 53 | + t.Run("with too many pushes", func(t *testing.T) { |
| 54 | + p1 := make(chan NID, 10) |
| 55 | + p1 <- NID{0xaa, 0xaa} |
| 56 | + p1 <- NID{0xbb, 0xbb} //with the given params this is too much push |
| 57 | + |
| 58 | + v2 := Brahms(NID{0xff, 0xff}, r, p, time.Millisecond*10, s, tr0, p1, v0) |
| 59 | + |
| 60 | + //with too many pushes the view shouldn't have changed |
| 61 | + test.Equals(t, v2, v0) |
| 62 | + }) |
| 63 | +} |
| 64 | + |
| 65 | +func TestBrahmsWithPullsAndPushes(t *testing.T) { |
| 66 | + p, _ := NewParams(0.1, 0.7, 0.2, 10, 2) |
| 67 | + r := rand.New(rand.NewSource(1)) |
| 68 | + s := NewSampler(r, p.l2()) |
| 69 | + self := NID{0x01} |
| 70 | + other := NID{0x02} |
| 71 | + |
| 72 | + v0 := NewView(other) |
| 73 | + pull1 := NID{0x01, 0x02} |
| 74 | + push1 := NID{0x02, 0x02} |
| 75 | + p0 := make(chan NID, 10) |
| 76 | + p0 <- push1 |
| 77 | + tr0 := NewMockTransport() |
| 78 | + tr0.SetPull(other, NewView(pull1, pull1)) |
| 79 | + |
| 80 | + //with both pushes and pulls the view should get updated |
| 81 | + v1 := Brahms(self, r, p, time.Millisecond*10, s, tr0, p0, v0) |
| 82 | + test.Equals(t, NewView(pull1, push1), v1) |
| 83 | + test.Equals(t, NewView(pull1, push1), s.Sample()) |
| 84 | +} |
0 commit comments