Developing lightweight computation at the DSG edge

antidoteclient_test.go 2.79 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
23
24
	bucket := Bucket{[]byte("bucket")}
	key := []byte("keyCounter")
	_, 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
56
57
58

	bucket := Bucket{[]byte("bucket")}
	key := []byte("keySet")

	_, err = bucket.Update(tx, SetAdd(key, []byte("test2")))
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
88
	bucket := Bucket{[]byte("bucket")}
	key := []byte("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
108
109
110
111
112
113
114
115
116
117
118
				_, err = tx.Commit()
				if err != nil {
					t.Fatal(err)
				}
				if i%1000 == 0 {
					fmt.Println(i)
				}
			}
		}()
	}
	wg.Wait()

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

Mathias Weber's avatar
Mathias Weber committed
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
	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 := []byte("keyMany")
	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
144
145
}

146
147
148
149
150
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
151
152
153
154
	bucket := Bucket{[]byte("bucket")}
	key := []byte("keyStatic")
	tx := client.CreateStaticTransaction()

155

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

Mathias Weber's avatar
Mathias Weber committed
165
	fmt.Print(counterVal)
166
}