|
| 1 | +package distribution |
| 2 | + |
| 3 | +import "testing" |
| 4 | + |
| 5 | +func TestEngineRouteLookup(t *testing.T) { |
| 6 | + e := NewEngine() |
| 7 | + e.UpdateRoute([]byte("a"), []byte("m"), 1) |
| 8 | + // second range has no upper bound |
| 9 | + e.UpdateRoute([]byte("m"), nil, 2) |
| 10 | + |
| 11 | + cases := []struct { |
| 12 | + key []byte |
| 13 | + group uint64 |
| 14 | + expect bool |
| 15 | + }{ |
| 16 | + {[]byte("a"), 1, true}, // start is inclusive |
| 17 | + {[]byte("b"), 1, true}, |
| 18 | + {[]byte("m"), 2, true}, // end is exclusive, so m belongs to second range |
| 19 | + {[]byte("x"), 2, true}, |
| 20 | + {[]byte("za"), 2, true}, // unbounded range covers keys beyond z |
| 21 | + } |
| 22 | + |
| 23 | + for _, c := range cases { |
| 24 | + r, ok := e.GetRoute(c.key) |
| 25 | + if ok != c.expect { |
| 26 | + t.Fatalf("key %q expected ok=%v, got %v", c.key, c.expect, ok) |
| 27 | + } |
| 28 | + if ok && r.GroupID != c.group { |
| 29 | + t.Fatalf("key %q expected group %d, got %d", c.key, c.group, r.GroupID) |
| 30 | + } |
| 31 | + } |
| 32 | +} |
| 33 | + |
| 34 | +func TestEngineTimestampMonotonic(t *testing.T) { |
| 35 | + e := NewEngine() |
| 36 | + last := e.NextTimestamp() |
| 37 | + for i := 0; i < 100; i++ { |
| 38 | + ts := e.NextTimestamp() |
| 39 | + if ts <= last { |
| 40 | + t.Fatalf("timestamp not monotonic: %d <= %d", ts, last) |
| 41 | + } |
| 42 | + last = ts |
| 43 | + } |
| 44 | +} |
0 commit comments