Developing lightweight computation at the DSG edge

antidoteclient_test.go 8.45 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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
func TestSetUpdateRemove(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("keySet")

	err = bucket.Update(tx, SetAdd(key, []byte("test1"), []byte("value2"), []byte("inset3")))
	if err != nil {
		t.Fatal(err)
	}

	err = tx.Commit()
	if err != nil {
		t.Fatal(err)
	}

	tx, err = client.StartTransaction()
	if err != nil {
		t.Fatal(err)
	}
	err = bucket.Update(tx, SetRemove(key, []byte("test1")))
	if err != nil {
		t.Fatal(err)
	}
	err = tx.Commit()
	if err != nil {
		t.Fatal(err)
	}

	tx, err = client.StartTransaction()
	if err != nil {
		t.Fatal(err)
	}
	setVal, err := bucket.ReadSet(tx, key)
	if err != nil {
		t.Fatal(err)
	}

	err = tx.Commit()
	if err != nil {
		t.Fatal(err)
	}

	if len(setVal) != 2 {
		t.Fatalf("Wrong size: %s", setVal)
	}
	for _, expected := range []string{"value2", "inset3"} {
		found := false
		for _, val := range setVal {
			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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
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
184
185
186
187
	if err != nil {
		t.Fatal(err)
	}

Mathias Weber's avatar
Mathias Weber committed
188
189
190
191
192
193
194
	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
195
	if v, e := mapVal.Reg(Key("reg")); e != nil || !bytes.Equal(v, []byte("Hello World")) {
Mathias Weber's avatar
Mathias Weber committed
196
197
		t.Fatalf("Wrong reg value: %p", v)
	}
Mathias Weber's avatar
Mathias Weber committed
198
	v, _ := mapVal.Set(Key("set"))
Mathias Weber's avatar
Mathias Weber committed
199
200
201
	if len(v) != 2 {
		t.Fatal("Wrong number of elements in set")
	}
Mathias Weber's avatar
Mathias Weber committed
202
	for _, expected := range []string{"A", "B"} {
Mathias Weber's avatar
Mathias Weber committed
203
		found := false
Mathias Weber's avatar
Mathias Weber committed
204
		for _, val := range v {
Mathias Weber's avatar
Mathias Weber committed
205
206
207
208
209
210
211
212
213
			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
214
}
215

216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
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
246
func testManyUpdates(t *testing.T) {
Mathias Weber's avatar
Mathias Weber committed
247
248
249
250
	client, err := NewClient(Host{"127.0.0.1", 8087})
	if err != nil {
		t.Fatal(err)
	}
Mathias Weber's avatar
Mathias Weber committed
251
	defer client.Close()
Mathias Weber's avatar
Mathias Weber committed
252

Mathias Weber's avatar
Mathias Weber committed
253
	bucket := Bucket{[]byte("bucket")}
Mathias Weber's avatar
Mathias Weber committed
254
	key := Key("keyMany")
Mathias Weber's avatar
Mathias Weber committed
255
256

	wg := sync.WaitGroup{}
257

Mathias Weber's avatar
Mathias Weber committed
258
	numThreads := 5
259
260

	wg.Add(numThreads)
Mathias Weber's avatar
Mathias Weber committed
261
	for k := 0; k < numThreads; k++ {
Mathias Weber's avatar
Mathias Weber committed
262
263
		go func() {
			defer wg.Done()
Mathias Weber's avatar
Mathias Weber committed
264
			for i := 0; i < 6000; i++ {
Mathias Weber's avatar
Mathias Weber committed
265
266
267
268
				tx, err := client.StartTransaction()
				if err != nil {
					t.Fatal(err)
				}
Mathias Weber's avatar
Mathias Weber committed
269
				err = bucket.Update(tx, CounterInc(key, 1))
Mathias Weber's avatar
Mathias Weber committed
270
271
272
				if err != nil {
					t.Fatal(err)
				}
Mathias Weber's avatar
Mathias Weber committed
273
				err = tx.Commit()
Mathias Weber's avatar
Mathias Weber committed
274
275
276
277
278
279
280
281
282
283
284
				if err != nil {
					t.Fatal(err)
				}
				if i%1000 == 0 {
					fmt.Println(i)
				}
			}
		}()
	}
	wg.Wait()

Mathias Weber's avatar
Mathias Weber committed
285
286
	tx := client.CreateStaticTransaction()
	counterVal, err := bucket.ReadCounter(tx, key)
Mathias Weber's avatar
Mathias Weber committed
287
288
289
290
	if err != nil {
		t.Fatal(err)
	}

Mathias Weber's avatar
Mathias Weber committed
291
292
293
	fmt.Print(counterVal)
}

294
295
// not as fast as the parallel version, but close
func testManyUpdatesSeq(t *testing.T) {
Mathias Weber's avatar
Mathias Weber committed
296
297
298
299
300
301
302
	client, err := NewClient(Host{"127.0.0.1", 8087})
	if err != nil {
		t.Fatal(err)
	}
	defer client.Close()

	bucket := Bucket{[]byte("bucket")}
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
	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
323
324
325
326
327
328
329
	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
330
331
}

332
333
// do not issue too many operations in one transaction, it will blow up the transaction cache!
func testManyUpdatesSeqInTrans(t *testing.T) {
334
335
336
337
	client, err := NewClient(Host{"127.0.0.1", 8087})
	if err != nil {
		t.Fatal(err)
	}
Mathias Weber's avatar
Mathias Weber committed
338
	defer client.Close()
Mathias Weber's avatar
Mathias Weber committed
339

340
341
342
343
	bucket := Bucket{[]byte("bucket")}
	key := Key("keyManySeqTrans")

	tx, err := client.StartTransaction()
344
345
346
	if err != nil {
		t.Fatal(err)
	}
347
348
349
350
351
352
353
354
355
356
357
	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()
358
359
360
361
	if err != nil {
		t.Fatal(err)
	}

362
363
364
365
	txs := client.CreateStaticTransaction()
	counterVal, err := bucket.ReadCounter(txs, key)
	if err != nil {
		t.Fatal(err)
Mathias Weber's avatar
Mathias Weber committed
366
	}
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386

	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)
387
}
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434

func TestMapListMapKeys(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"))))
	if err != nil {
		t.Fatal(err)
	}

	mapV, err := bucket.ReadMap(tx, key)
	if err != nil {
		t.Fatal(err)
	}
	keyList := mapV.ListMapKeys()
	for _, expected := range []MapEntryKey{
		{[]byte("counter"), CRDTType_COUNTER},
		{[]byte("reg"), CRDTType_LWWREG},
		{[]byte("set"), CRDTType_ORSET},
	} {
		found := false
		for _, entry := range keyList {
			if bytes.Equal(entry.Key, expected.Key) && entry.CrdtType == expected.CrdtType {
				found = true
				break
			}
		}
		if !found {
			t.Fatalf("expected value %s not found in result (%s)", expected.Key, keyList)
		}
	}
}