blob: 780eff52a7d4632d0ffed61a1c61b48670f67702 [file] [log] [blame]
Cosmos Nicolaou174a44e2014-05-16 13:59:30 -07001package ipc
2
3import (
Cosmos Nicolaou174a44e2014-05-16 13:59:30 -07004 "sort"
5 "sync"
6 "testing"
Jiri Simsa870ddd62014-06-27 14:56:58 -07007
Jiri Simsa519c5072014-09-17 21:37:57 -07008 "veyron.io/veyron/veyron/lib/testutil"
Cosmos Nicolaou174a44e2014-05-16 13:59:30 -07009)
10
Asim Shankarc920db32014-10-16 19:18:21 -070011func init() { testutil.Init() }
12
Cosmos Nicolaou174a44e2014-05-16 13:59:30 -070013func randomKeys() []uint64 {
Jiri Simsa870ddd62014-06-27 14:56:58 -070014 n := (testutil.Rand.Intn(256*10) / 10) + 256
Cosmos Nicolaou174a44e2014-05-16 13:59:30 -070015 k := make([]uint64, n)
16 for i := 0; i < n; i++ {
Jiri Simsa870ddd62014-06-27 14:56:58 -070017 k[i] = uint64(testutil.Rand.Int63())
Cosmos Nicolaou174a44e2014-05-16 13:59:30 -070018 }
19 return k
20}
21
22type keySlice []uint64
23
24func (p keySlice) Len() int { return len(p) }
25func (p keySlice) Less(i, j int) bool { return p[i] < p[j] }
26func (p keySlice) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
27func (p keySlice) Sort() { sort.Sort(p) }
28
29func 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
50func 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
76func 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
94func 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 Nicolaou174a44e2014-05-16 13:59:30 -0700115func 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 Shankarc14225c2014-05-23 17:18:36 -0700122 if r := store.waitForEntry(id); r != nil {
123 t.Errorf("Got %v, want nil", r)
Cosmos Nicolaou174a44e2014-05-16 13:59:30 -0700124 }
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 Shankarc14225c2014-05-23 17:18:36 -0700134
135func 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}