Developing lightweight computation at the DSG edge

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

//import "fmt"
import (
	"testing"
	"fmt"
Mathias Weber's avatar
Mathias Weber committed
7
	"sync"
Peter Zeller's avatar
Peter Zeller committed
8
9
10
11
12
13
14
15
)

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
16
	defer client.Close()
Peter Zeller's avatar
Peter Zeller committed
17
18
19
20
21

	tx, err := client.StartTransaction()
	if err != nil {
		t.Fatal(err)
	}
Mathias Weber's avatar
Mathias Weber committed
22
	bucket := Bucket{[]byte("bucket")}
Mathias Weber's avatar
Mathias Weber committed
23
	key := Key("keyCounter")
Mathias Weber's avatar
Mathias Weber committed
24
	err = bucket.Update(tx, CounterInc(key, 1))
Mathias Weber's avatar
Mathias Weber committed
25
26
27
28
	if err != nil {
		t.Fatal(err)
	}

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

	fmt.Print(counterVal)
Mathias Weber's avatar
Mathias Weber committed
35

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

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
48
	defer client.Close()
Mathias Weber's avatar
Mathias Weber committed
49
50
51
52
53

	tx, err := client.StartTransaction()
	if err != nil {
		t.Fatal(err)
	}
Mathias Weber's avatar
Mathias Weber committed
54
55

	bucket := Bucket{[]byte("bucket")}
Mathias Weber's avatar
Mathias Weber committed
56
	key := Key("keySet")
Mathias Weber's avatar
Mathias Weber committed
57

Mathias Weber's avatar
Mathias Weber committed
58
	err = bucket.Update(tx, SetAdd(key, []byte("test1")))
Peter Zeller's avatar
Peter Zeller committed
59
60
61
62
	if err != nil {
		t.Fatal(err)
	}

Mathias Weber's avatar
Mathias Weber committed
63
64
65
66
67
68
69
70
71

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

	for _,v := range setVal {
		fmt.Println(string(v))
	}
Peter Zeller's avatar
Peter Zeller committed
72

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

}
79

Mathias Weber's avatar
Mathias Weber committed
80
81
82
83
84
func TestManyUpdates(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
85
	defer client.Close()
Mathias Weber's avatar
Mathias Weber committed
86

Mathias Weber's avatar
Mathias Weber committed
87
	bucket := Bucket{[]byte("bucket")}
Mathias Weber's avatar
Mathias Weber committed
88
	key := Key("keyMany")
Mathias Weber's avatar
Mathias Weber committed
89
90
91
92
93
94
95
96
97
98
99

	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
100
				err = bucket.Update(tx, CounterInc(key, 1))
Mathias Weber's avatar
Mathias Weber committed
101
102
103
				if err != nil {
					t.Fatal(err)
				}
Mathias Weber's avatar
Mathias Weber committed
104
				err = tx.Commit()
Mathias Weber's avatar
Mathias Weber committed
105
106
107
108
109
110
111
112
113
114
115
				if err != nil {
					t.Fatal(err)
				}
				if i%1000 == 0 {
					fmt.Println(i)
				}
			}
		}()
	}
	wg.Wait()

Mathias Weber's avatar
Mathias Weber committed
116
117
	tx := client.CreateStaticTransaction()
	counterVal, err := bucket.ReadCounter(tx, key)
Mathias Weber's avatar
Mathias Weber committed
118
119
120
121
	if err != nil {
		t.Fatal(err)
	}

Mathias Weber's avatar
Mathias Weber committed
122
123
124
125
126
127
128
129
130
131
132
	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")}
Mathias Weber's avatar
Mathias Weber committed
133
	key := Key("keyMany")
Mathias Weber's avatar
Mathias Weber committed
134
135
136
137
138
139
140
	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
141
142
}

143
144
145
146
147
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
148
	bucket := Bucket{[]byte("bucket")}
Mathias Weber's avatar
Mathias Weber committed
149
	key := Key("keyStatic")
Mathias Weber's avatar
Mathias Weber committed
150
151
	tx := client.CreateStaticTransaction()

152

Mathias Weber's avatar
Mathias Weber committed
153
	err = bucket.Update(tx, CounterInc(key, 1))
154
155
156
	if err != nil {
		t.Fatal(err)
	}
Mathias Weber's avatar
Mathias Weber committed
157
	counterVal, err := bucket.ReadCounter(tx, key)
158
159
160
161
	if err != nil {
		t.Fatal(err)
	}

Mathias Weber's avatar
Mathias Weber committed
162
	fmt.Print(counterVal)
163
}