Developing lightweight computation at the DSG edge

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

import (
	"testing"
	"fmt"
Mathias Weber's avatar
Mathias Weber committed
6
	"sync"
Mathias Weber's avatar
Mathias Weber committed
7
8
	"bytes"
	"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
45

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

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
54
	defer client.Close()
Mathias Weber's avatar
Mathias Weber committed
55
56
57
58
59

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

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

Mathias Weber's avatar
Mathias Weber committed
70
71
72
73
74
75

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

Mathias Weber's avatar
Mathias Weber committed
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
	err = tx.Commit()
	if err != nil {
		t.Fatal(err)
	}

	
	for _,expected := range []string{"test1", "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
93
	}
Mathias Weber's avatar
Mathias Weber committed
94
}
Peter Zeller's avatar
Peter Zeller committed
95

Mathias Weber's avatar
Mathias Weber committed
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
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
116
117
118
119
	if err != nil {
		t.Fatal(err)
	}

Mathias Weber's avatar
Mathias Weber committed
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
	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)
	}
	if v,e := mapVal.Reg(Key("reg")); e != nil || !bytes.Equal(v, []byte("Hello World")) {
		t.Fatalf("Wrong reg value: %p", v)
	}
	v,_ := mapVal.Set(Key("set"))
	if len(v) != 2 {
		t.Fatal("Wrong number of elements in set")
	}
	for _,expected := range []string{"A", "B"} {
		found := false
		for _,val := range v {
			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
146
}
147

Mathias Weber's avatar
Mathias Weber committed
148
func testManyUpdates(t *testing.T) {
Mathias Weber's avatar
Mathias Weber committed
149
150
151
152
	client, err := NewClient(Host{"127.0.0.1", 8087})
	if err != nil {
		t.Fatal(err)
	}
Mathias Weber's avatar
Mathias Weber committed
153
	defer client.Close()
Mathias Weber's avatar
Mathias Weber committed
154

Mathias Weber's avatar
Mathias Weber committed
155
	bucket := Bucket{[]byte("bucket")}
Mathias Weber's avatar
Mathias Weber committed
156
	key := Key("keyMany")
Mathias Weber's avatar
Mathias Weber committed
157
158
159
160
161
162
163
164
165
166
167

	wg := sync.WaitGroup{}
	wg.Add(10)
	for k:=0; k<10; k++ {
		go func() {
			defer wg.Done()
			for i := 0; i < 10000; i++ {
				tx, err := client.StartTransaction()
				if err != nil {
					t.Fatal(err)
				}
Mathias Weber's avatar
Mathias Weber committed
168
				err = bucket.Update(tx, CounterInc(key, 1))
Mathias Weber's avatar
Mathias Weber committed
169
170
171
				if err != nil {
					t.Fatal(err)
				}
Mathias Weber's avatar
Mathias Weber committed
172
				err = tx.Commit()
Mathias Weber's avatar
Mathias Weber committed
173
174
175
176
177
178
179
180
181
182
183
				if err != nil {
					t.Fatal(err)
				}
				if i%1000 == 0 {
					fmt.Println(i)
				}
			}
		}()
	}
	wg.Wait()

Mathias Weber's avatar
Mathias Weber committed
184
185
	tx := client.CreateStaticTransaction()
	counterVal, err := bucket.ReadCounter(tx, key)
Mathias Weber's avatar
Mathias Weber committed
186
187
188
189
	if err != nil {
		t.Fatal(err)
	}

Mathias Weber's avatar
Mathias Weber committed
190
191
192
	fmt.Print(counterVal)
}

Mathias Weber's avatar
Mathias Weber committed
193
func testReadMany(t *testing.T) {
Mathias Weber's avatar
Mathias Weber committed
194
195
196
197
198
199
200
	client, err := NewClient(Host{"127.0.0.1", 8087})
	if err != nil {
		t.Fatal(err)
	}
	defer client.Close()

	bucket := Bucket{[]byte("bucket")}
Mathias Weber's avatar
Mathias Weber committed
201
	key := Key("keyMany")
Mathias Weber's avatar
Mathias Weber committed
202
203
204
205
206
207
208
	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
209
210
}

211
212
213
214
215
func TestStatic(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
216
217
218
219
220
	defer client.Close()
	
	timestamp := time.Now().Unix()
	bucketname := fmt.Sprintf("bucket%d", timestamp)
	bucket := Bucket{[]byte(bucketname)}
Mathias Weber's avatar
Mathias Weber committed
221
	key := Key("keyStatic")
Mathias Weber's avatar
Mathias Weber committed
222
223
	tx := client.CreateStaticTransaction()

Mathias Weber's avatar
Mathias Weber committed
224
	err = bucket.Update(tx, CounterInc(key, 42))
225
226
227
	if err != nil {
		t.Fatal(err)
	}
Mathias Weber's avatar
Mathias Weber committed
228
	counterVal, err := bucket.ReadCounter(tx, key)
229
230
231
232
	if err != nil {
		t.Fatal(err)
	}

Mathias Weber's avatar
Mathias Weber committed
233
234
235
	if counterVal != 42 {
		t.Fatalf("Counter value should be 42 but is %d", counterVal)
	}
236
}