Cosmos Nicolaou | 174a44e | 2014-05-16 13:59:30 -0700 | [diff] [blame] | 1 | package ipc |
| 2 | |
| 3 | import ( |
Cosmos Nicolaou | 174a44e | 2014-05-16 13:59:30 -0700 | [diff] [blame] | 4 | "sort" |
| 5 | "sync" |
| 6 | "testing" |
Jiri Simsa | 870ddd6 | 2014-06-27 14:56:58 -0700 | [diff] [blame] | 7 | |
Jiri Simsa | 519c507 | 2014-09-17 21:37:57 -0700 | [diff] [blame] | 8 | "veyron.io/veyron/veyron/lib/testutil" |
Cosmos Nicolaou | 174a44e | 2014-05-16 13:59:30 -0700 | [diff] [blame] | 9 | ) |
| 10 | |
Asim Shankar | c920db3 | 2014-10-16 19:18:21 -0700 | [diff] [blame] | 11 | func init() { testutil.Init() } |
| 12 | |
Cosmos Nicolaou | 174a44e | 2014-05-16 13:59:30 -0700 | [diff] [blame] | 13 | func randomKeys() []uint64 { |
Jiri Simsa | 870ddd6 | 2014-06-27 14:56:58 -0700 | [diff] [blame] | 14 | n := (testutil.Rand.Intn(256*10) / 10) + 256 |
Cosmos Nicolaou | 174a44e | 2014-05-16 13:59:30 -0700 | [diff] [blame] | 15 | k := make([]uint64, n) |
| 16 | for i := 0; i < n; i++ { |
Jiri Simsa | 870ddd6 | 2014-06-27 14:56:58 -0700 | [diff] [blame] | 17 | k[i] = uint64(testutil.Rand.Int63()) |
Cosmos Nicolaou | 174a44e | 2014-05-16 13:59:30 -0700 | [diff] [blame] | 18 | } |
| 19 | return k |
| 20 | } |
| 21 | |
| 22 | type keySlice []uint64 |
| 23 | |
| 24 | func (p keySlice) Len() int { return len(p) } |
| 25 | func (p keySlice) Less(i, j int) bool { return p[i] < p[j] } |
| 26 | func (p keySlice) Swap(i, j int) { p[i], p[j] = p[j], p[i] } |
| 27 | func (p keySlice) Sort() { sort.Sort(p) } |
| 28 | |
| 29 | func TestStoreRandom(t *testing.T) { |
| 30 | store := newStore() |
| 31 | keys := randomKeys() |
| 32 | |
| 33 | for i := 0; i < len(keys); i++ { |
| 34 | r := []interface{}{i} |
| 35 | store.addEntry(keys[i], r) |
| 36 | } |
| 37 | if len(store.store) != len(keys) { |
| 38 | t.Errorf("num stored entries: got %d, want %d", len(store.store), len(keys)) |
| 39 | } |
| 40 | for i := 0; i < len(keys); i++ { |
| 41 | // Each call to removeEntries will remove an unknown number of entries |
| 42 | // depending on the original randomised value of the ints. |
| 43 | store.removeEntriesTo(keys[i]) |
| 44 | } |
| 45 | if len(store.store) != 0 { |
| 46 | t.Errorf("store is not empty: %d", len(store.store)) |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | func TestStoreOrdered(t *testing.T) { |
| 51 | store := newStore() |
| 52 | keys := randomKeys() |
| 53 | |
| 54 | for i := 0; i < len(keys); i++ { |
| 55 | r := []interface{}{i} |
| 56 | store.addEntry(keys[i], r) |
| 57 | } |
| 58 | if len(store.store) != len(keys) { |
| 59 | t.Errorf("num stored entries: got %d, want %d", len(store.store), len(keys)) |
| 60 | } |
| 61 | |
| 62 | (keySlice(keys)).Sort() |
| 63 | l := len(keys) |
| 64 | for i := 0; i < len(keys); i++ { |
| 65 | store.removeEntriesTo(keys[i]) |
| 66 | l-- |
| 67 | if len(store.store) != l { |
| 68 | t.Errorf("failed to remove a single item(%d): %d != %d", keys[i], len(store.store), l) |
| 69 | } |
| 70 | } |
| 71 | if len(store.store) != 0 { |
| 72 | t.Errorf("store is not empty: %d", len(store.store)) |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | func TestStoreWaitForEntry(t *testing.T) { |
| 77 | store := newStore() |
| 78 | store.addEntry(1, []interface{}{"1"}) |
| 79 | r := store.waitForEntry(1) |
| 80 | if r[0].(string) != "1" { |
| 81 | t.Errorf("Got: %q, Want: %q", r[0], "1") |
| 82 | } |
| 83 | ch := make(chan string) |
| 84 | go func(ch chan string) { |
| 85 | r := store.waitForEntry(2) |
| 86 | ch <- r[0].(string) |
| 87 | }(ch) |
| 88 | store.addEntry(2, []interface{}{"2"}) |
| 89 | if result := <-ch; result != "2" { |
| 90 | t.Errorf("Got: %q, Want: %q", r[0], "2") |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | func TestStoreWaitForEntryRandom(t *testing.T) { |
| 95 | store := newStore() |
| 96 | keys := randomKeys() |
| 97 | var wg sync.WaitGroup |
| 98 | for _, k := range keys { |
| 99 | wg.Add(1) |
| 100 | go func(t *testing.T, id uint64) { |
| 101 | r := store.waitForEntry(id) |
| 102 | if r[0].(uint64) != id { |
| 103 | t.Errorf("Got: %d, Want: %d", r[0].(uint64), id) |
| 104 | } |
| 105 | wg.Done() |
| 106 | }(t, k) |
| 107 | } |
| 108 | (keySlice(keys)).Sort() |
| 109 | for _, k := range keys { |
| 110 | store.addEntry(k, []interface{}{k}) |
| 111 | } |
| 112 | wg.Wait() |
| 113 | } |
| 114 | |
Cosmos Nicolaou | 174a44e | 2014-05-16 13:59:30 -0700 | [diff] [blame] | 115 | func TestStoreWaitForRemovedEntry(t *testing.T) { |
| 116 | store := newStore() |
| 117 | keys := randomKeys() |
| 118 | var wg sync.WaitGroup |
| 119 | for _, k := range keys { |
| 120 | wg.Add(1) |
| 121 | go func(t *testing.T, id uint64) { |
Asim Shankar | c14225c | 2014-05-23 17:18:36 -0700 | [diff] [blame] | 122 | if r := store.waitForEntry(id); r != nil { |
| 123 | t.Errorf("Got %v, want nil", r) |
Cosmos Nicolaou | 174a44e | 2014-05-16 13:59:30 -0700 | [diff] [blame] | 124 | } |
| 125 | wg.Done() |
| 126 | }(t, k) |
| 127 | } |
| 128 | (keySlice(keys)).Sort() |
| 129 | for _, k := range keys { |
| 130 | store.removeEntriesTo(k) |
| 131 | } |
| 132 | wg.Wait() |
| 133 | } |
Asim Shankar | c14225c | 2014-05-23 17:18:36 -0700 | [diff] [blame] | 134 | |
| 135 | func TestStoreWaitForOldEntry(t *testing.T) { |
| 136 | store := newStore() |
| 137 | store.addEntry(1, []interface{}{"result"}) |
| 138 | store.removeEntriesTo(1) |
| 139 | if got := store.waitForEntry(1); got != nil { |
| 140 | t.Errorf("Got %T=%v, want nil", got, got) |
| 141 | } |
| 142 | } |