Developing lightweight computation at the DSG edge

antidoteclient_test.go 2.87 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
36
37
38
39
40
41
42
43
44
45
46
47

	_, err = tx.Commit()
	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

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
				updateResp, 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
105
106
				if !(*updateResp.Success) {
					fmt.Printf("Update #%d not successful\n", i)
				}
Mathias Weber's avatar
Mathias Weber committed
107
				commitResp, err := tx.Commit()
Mathias Weber's avatar
Mathias Weber committed
108
109
110
				if err != nil {
					t.Fatal(err)
				}
Mathias Weber's avatar
Mathias Weber committed
111
112
113
				if !(*commitResp.Success) {
					fmt.Printf("Commit #%d not successful\n", i)
				}
Mathias Weber's avatar
Mathias Weber committed
114
115
116
117
118
119
120
121
				if i%1000 == 0 {
					fmt.Println(i)
				}
			}
		}()
	}
	wg.Wait()

Mathias Weber's avatar
Mathias Weber committed
122
123
	tx := client.CreateStaticTransaction()
	counterVal, err := bucket.ReadCounter(tx, key)
Mathias Weber's avatar
Mathias Weber committed
124
125
126
127
	if err != nil {
		t.Fatal(err)
	}

Mathias Weber's avatar
Mathias Weber committed
128
129
130
131
132
133
134
135
136
137
138
	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
139
	key := Key("keyMany")
Mathias Weber's avatar
Mathias Weber committed
140
141
142
143
144
145
146
	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
147
148
}

149
150
151
152
153
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
154
	bucket := Bucket{[]byte("bucket")}
Mathias Weber's avatar
Mathias Weber committed
155
	key := Key("keyStatic")
Mathias Weber's avatar
Mathias Weber committed
156
157
	tx := client.CreateStaticTransaction()

158

Mathias Weber's avatar
Mathias Weber committed
159
	_, err = bucket.Update(tx, CounterInc(key, 1))
160
161
162
	if err != nil {
		t.Fatal(err)
	}
Mathias Weber's avatar
Mathias Weber committed
163
	counterVal, err := bucket.ReadCounter(tx, key)
164
165
166
167
	if err != nil {
		t.Fatal(err)
	}

Mathias Weber's avatar
Mathias Weber committed
168
	fmt.Print(counterVal)
169
}