Developing lightweight computation at the DSG edge

antidoteclient_test.go 6.09 KB
Newer Older
Peter Zeller's avatar
Peter Zeller committed
1
2
3
package antidoteclient

import (
Mathias Weber's avatar
Mathias Weber committed
4
	"bytes"
Peter Zeller's avatar
Peter Zeller committed
5
	"fmt"
Mathias Weber's avatar
Mathias Weber committed
6
	"sync"
Mathias Weber's avatar
Mathias Weber committed
7
	"testing"
Mathias Weber's avatar
Mathias Weber committed
8
	"time"
Peter Zeller's avatar
Peter Zeller committed
9
10
11
12
13
14
15
16
)

func TestSimple(t *testing.T) {

	client, err := NewClient(Host{"127.0.0.1", 8087})
	if err != nil {
		t.Fatal(err)
	}
Mathias Weber's avatar
Mathias Weber committed
17
	defer client.Close()
Peter Zeller's avatar
Peter Zeller committed
18
19
20
21
22

	tx, err := client.StartTransaction()
	if err != nil {
		t.Fatal(err)
	}
Mathias Weber's avatar
Mathias Weber committed
23
24
25
	timestamp := time.Now().Unix()
	bucketname := fmt.Sprintf("bucket%d", timestamp)
	bucket := Bucket{[]byte(bucketname)}
Mathias Weber's avatar
Mathias Weber committed
26
	key := Key("keyCounter")
Mathias Weber's avatar
Mathias Weber committed
27
	err = bucket.Update(tx, CounterInc(key, 1))
Mathias Weber's avatar
Mathias Weber committed
28
29
30
31
	if err != nil {
		t.Fatal(err)
	}

Mathias Weber's avatar
Mathias Weber committed
32
33
34
35
36
	counterVal, err := bucket.ReadCounter(tx, key)
	if err != nil {
		t.Fatal(err)
	}

Mathias Weber's avatar
Mathias Weber committed
37
	err = tx.Commit()
Mathias Weber's avatar
Mathias Weber committed
38
39
40
	if err != nil {
		t.Fatal(err)
	}
Mathias Weber's avatar
Mathias Weber committed
41
42
43
44

	if counterVal != 1 {
		t.Fatalf("Counter value should be 1 but is %d", counterVal)
	}
Mathias Weber's avatar
Mathias Weber committed
45
46
47
48
49
50
51
52
}

func TestSetUpdate(t *testing.T) {

	client, err := NewClient(Host{"127.0.0.1", 8087})
	if err != nil {
		t.Fatal(err)
	}
Mathias Weber's avatar
Mathias Weber committed
53
	defer client.Close()
Mathias Weber's avatar
Mathias Weber committed
54
55
56
57
58

	tx, err := client.StartTransaction()
	if err != nil {
		t.Fatal(err)
	}
Mathias Weber's avatar
Mathias Weber committed
59
60
61
	timestamp := time.Now().Unix()
	bucketname := fmt.Sprintf("bucket%d", timestamp)
	bucket := Bucket{[]byte(bucketname)}
Mathias Weber's avatar
Mathias Weber committed
62
	key := Key("keySet")
Mathias Weber's avatar
Mathias Weber committed
63

Mathias Weber's avatar
Mathias Weber committed
64
	err = bucket.Update(tx, SetAdd(key, []byte("test1"), []byte("value2"), []byte("inset3")))
Peter Zeller's avatar
Peter Zeller committed
65
66
67
68
	if err != nil {
		t.Fatal(err)
	}

Mathias Weber's avatar
Mathias Weber committed
69
70
71
72
73
	setVal, err := bucket.ReadSet(tx, key)
	if err != nil {
		t.Fatal(err)
	}

Mathias Weber's avatar
Mathias Weber committed
74
75
76
77
78
	err = tx.Commit()
	if err != nil {
		t.Fatal(err)
	}

Mathias Weber's avatar
Mathias Weber committed
79
	for _, expected := range []string{"test1", "value2", "inset3"} {
Mathias Weber's avatar
Mathias Weber committed
80
		found := false
Mathias Weber's avatar
Mathias Weber committed
81
		for _, val := range setVal {
Mathias Weber's avatar
Mathias Weber committed
82
83
84
85
86
87
88
89
			if string(val) == expected {
				found = true
				break
			}
		}
		if !found {
			t.Fatalf("expected value %s not found in result (%s)", expected, setVal)
		}
Mathias Weber's avatar
Mathias Weber committed
90
	}
Mathias Weber's avatar
Mathias Weber committed
91
}
Peter Zeller's avatar
Peter Zeller committed
92

Mathias Weber's avatar
Mathias Weber committed
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
func TestMap(t *testing.T) {
	client, err := NewClient(Host{"127.0.0.1", 8087})
	if err != nil {
		t.Fatal(err)
	}
	defer client.Close()

	tx, err := client.StartTransaction()
	if err != nil {
		t.Fatal(err)
	}
	timestamp := time.Now().Unix()
	bucketname := fmt.Sprintf("bucket%d", timestamp)
	bucket := Bucket{[]byte(bucketname)}
	key := Key("keyMap")

	err = bucket.Update(tx, MapUpdate(key,
		CounterInc(Key("counter"), 13),
		RegPut(Key("reg"), []byte("Hello World")),
		SetAdd(Key("set"), []byte("A"), []byte("B"))))
Peter Zeller's avatar
Peter Zeller committed
113
114
115
116
	if err != nil {
		t.Fatal(err)
	}

Mathias Weber's avatar
Mathias Weber committed
117
118
119
120
121
122
123
	mapVal, err := bucket.ReadMap(tx, key)
	if err != nil {
		t.Fatal(err)
	}
	if v, e := mapVal.Counter(Key("counter")); e != nil || v != 13 {
		t.Fatalf("Wrong counter value: %d", v)
	}
Mathias Weber's avatar
Mathias Weber committed
124
	if v, e := mapVal.Reg(Key("reg")); e != nil || !bytes.Equal(v, []byte("Hello World")) {
Mathias Weber's avatar
Mathias Weber committed
125
126
		t.Fatalf("Wrong reg value: %p", v)
	}
Mathias Weber's avatar
Mathias Weber committed
127
	v, _ := mapVal.Set(Key("set"))
Mathias Weber's avatar
Mathias Weber committed
128
129
130
	if len(v) != 2 {
		t.Fatal("Wrong number of elements in set")
	}
Mathias Weber's avatar
Mathias Weber committed
131
	for _, expected := range []string{"A", "B"} {
Mathias Weber's avatar
Mathias Weber committed
132
		found := false
Mathias Weber's avatar
Mathias Weber committed
133
		for _, val := range v {
Mathias Weber's avatar
Mathias Weber committed
134
135
136
137
138
139
140
141
142
			if string(val) == expected {
				found = true
				break
			}
		}
		if !found {
			t.Fatalf("expected value %s not found in result (%s)", expected, v)
		}
	}
Peter Zeller's avatar
Peter Zeller committed
143
}
144

145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
func TestStatic(t *testing.T) {
	client, err := NewClient(Host{"127.0.0.1", 8087})
	if err != nil {
		t.Fatal(err)
	}
	defer client.Close()

	timestamp := time.Now().Unix()
	bucketname := fmt.Sprintf("bucket%d", timestamp)
	bucket := Bucket{[]byte(bucketname)}
	key := Key("keyStatic")
	tx := client.CreateStaticTransaction()

	err = bucket.Update(tx, CounterInc(key, 42))
	if err != nil {
		t.Fatal(err)
	}
	counterVal, err := bucket.ReadCounter(tx, key)
	if err != nil {
		t.Fatal(err)
	}

	if counterVal != 42 {
		t.Fatalf("Counter value should be 42 but is %d", counterVal)
	}
}

// tests for many updates, not enabled

// this is a bit faster than the sequential one, if number of threads in configured correctly
Mathias Weber's avatar
Mathias Weber committed
175
func testManyUpdates(t *testing.T) {
Mathias Weber's avatar
Mathias Weber committed
176
177
178
179
	client, err := NewClient(Host{"127.0.0.1", 8087})
	if err != nil {
		t.Fatal(err)
	}
Mathias Weber's avatar
Mathias Weber committed
180
	defer client.Close()
Mathias Weber's avatar
Mathias Weber committed
181

Mathias Weber's avatar
Mathias Weber committed
182
	bucket := Bucket{[]byte("bucket")}
Mathias Weber's avatar
Mathias Weber committed
183
	key := Key("keyMany")
Mathias Weber's avatar
Mathias Weber committed
184
185

	wg := sync.WaitGroup{}
186

Mathias Weber's avatar
Mathias Weber committed
187
	numThreads := 5
188
189

	wg.Add(numThreads)
Mathias Weber's avatar
Mathias Weber committed
190
	for k := 0; k < numThreads; k++ {
Mathias Weber's avatar
Mathias Weber committed
191
192
		go func() {
			defer wg.Done()
Mathias Weber's avatar
Mathias Weber committed
193
			for i := 0; i < 6000; i++ {
Mathias Weber's avatar
Mathias Weber committed
194
195
196
197
				tx, err := client.StartTransaction()
				if err != nil {
					t.Fatal(err)
				}
Mathias Weber's avatar
Mathias Weber committed
198
				err = bucket.Update(tx, CounterInc(key, 1))
Mathias Weber's avatar
Mathias Weber committed
199
200
201
				if err != nil {
					t.Fatal(err)
				}
Mathias Weber's avatar
Mathias Weber committed
202
				err = tx.Commit()
Mathias Weber's avatar
Mathias Weber committed
203
204
205
206
207
208
209
210
211
212
213
				if err != nil {
					t.Fatal(err)
				}
				if i%1000 == 0 {
					fmt.Println(i)
				}
			}
		}()
	}
	wg.Wait()

Mathias Weber's avatar
Mathias Weber committed
214
215
	tx := client.CreateStaticTransaction()
	counterVal, err := bucket.ReadCounter(tx, key)
Mathias Weber's avatar
Mathias Weber committed
216
217
218
219
	if err != nil {
		t.Fatal(err)
	}

Mathias Weber's avatar
Mathias Weber committed
220
221
222
	fmt.Print(counterVal)
}

223
224
// not as fast as the parallel version, but close
func testManyUpdatesSeq(t *testing.T) {
Mathias Weber's avatar
Mathias Weber committed
225
226
227
228
229
230
231
	client, err := NewClient(Host{"127.0.0.1", 8087})
	if err != nil {
		t.Fatal(err)
	}
	defer client.Close()

	bucket := Bucket{[]byte("bucket")}
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
	key := Key("keyManySeq")

	for i := 0; i < 30000; i++ {
		tx, err := client.StartTransaction()
		if err != nil {
			t.Fatal(err)
		}
		err = bucket.Update(tx, CounterInc(key, 1))
		if err != nil {
			t.Fatal(err)
		}
		err = tx.Commit()
		if err != nil {
			t.Fatal(err)
		}
		if i%1000 == 0 {
			fmt.Println(i)
		}
	}

Mathias Weber's avatar
Mathias Weber committed
252
253
254
255
256
257
258
	tx := client.CreateStaticTransaction()
	counterVal, err := bucket.ReadCounter(tx, key)
	if err != nil {
		t.Fatal(err)
	}

	fmt.Print(counterVal)
Mathias Weber's avatar
Mathias Weber committed
259
260
}

261
262
// do not issue too many operations in one transaction, it will blow up the transaction cache!
func testManyUpdatesSeqInTrans(t *testing.T) {
263
264
265
266
	client, err := NewClient(Host{"127.0.0.1", 8087})
	if err != nil {
		t.Fatal(err)
	}
Mathias Weber's avatar
Mathias Weber committed
267
	defer client.Close()
Mathias Weber's avatar
Mathias Weber committed
268

269
270
271
272
	bucket := Bucket{[]byte("bucket")}
	key := Key("keyManySeqTrans")

	tx, err := client.StartTransaction()
273
274
275
	if err != nil {
		t.Fatal(err)
	}
276
277
278
279
280
281
282
283
284
285
286
	for i := 0; i < 30000; i++ {
		err = bucket.Update(tx, CounterInc(key, 1))
		if err != nil {
			t.Fatal(err)
		}

		if i%1000 == 0 {
			fmt.Println(i)
		}
	}
	err = tx.Commit()
287
288
289
290
	if err != nil {
		t.Fatal(err)
	}

291
292
293
294
	txs := client.CreateStaticTransaction()
	counterVal, err := bucket.ReadCounter(txs, key)
	if err != nil {
		t.Fatal(err)
Mathias Weber's avatar
Mathias Weber committed
295
	}
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315

	fmt.Print(counterVal)
}

func testReadMany(t *testing.T) {
	client, err := NewClient(Host{"127.0.0.1", 8087})
	if err != nil {
		t.Fatal(err)
	}
	defer client.Close()

	bucket := Bucket{[]byte("bucket")}
	key := Key("keyMany")
	tx := client.CreateStaticTransaction()
	counterVal, err := bucket.ReadCounter(tx, key)
	if err != nil {
		t.Fatal(err)
	}

	fmt.Print(counterVal)
316
}