Developing lightweight computation at the DSG edge

antidoteclient_test.go 2.84 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
16
17
18
19
20
)

func TestSimple(t *testing.T) {

	client, err := NewClient(Host{"127.0.0.1", 8087})
	if err != nil {
		t.Fatal(err)
	}

	tx, err := client.StartTransaction()
	if err != nil {
		t.Fatal(err)
	}
Mathias Weber's avatar
Mathias Weber committed
21
	crdtType := CRDTType_ORSET
Peter Zeller's avatar
Peter Zeller committed
22
23
	key := &ApbBoundObject{
		Bucket: []byte("bucket"),
Mathias Weber's avatar
Mathias Weber committed
24
		Key:    []byte("keySet"),
Peter Zeller's avatar
Peter Zeller committed
25
		Type:   &crdtType}
Mathias Weber's avatar
Mathias Weber committed
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
	addType := ApbSetUpdate_ADD
	tx.Update(&ApbUpdateOp{
		Boundobject: key,
		Operation:   &ApbUpdateOperation{Setop: &ApbSetUpdate{Optype: &addType, Adds: [][]byte{[]byte("test1")}}},
	})
	resp, err := tx.Read(key)
	if err != nil {
		t.Fatal(err)
	}

	fmt.Print(resp.Objects[0])

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

	client.Close()
}

func TestSetUpdate(t *testing.T) {

	client, err := NewClient(Host{"127.0.0.1", 8087})
	if err != nil {
		t.Fatal(err)
	}

	tx, err := client.StartTransaction()
	if err != nil {
		t.Fatal(err)
	}
	crdtType := CRDTType_ORSET
	key := &ApbBoundObject{
		Bucket: []byte("bucket"),
		Key:    []byte("keySet"),
		Type:   &crdtType}
	addType := ApbSetUpdate_ADD
63
	tx.Update(&ApbUpdateOp{
Peter Zeller's avatar
Peter Zeller committed
64
		Boundobject: key,
Mathias Weber's avatar
Mathias Weber committed
65
		Operation:   &ApbUpdateOperation{Setop: &ApbSetUpdate{Optype: &addType, Adds: [][]byte{[]byte("test1")}}},
Peter Zeller's avatar
Peter Zeller committed
66
	})
67
	resp, err := tx.Read(key)
Peter Zeller's avatar
Peter Zeller committed
68
69
70
71
72
73
	if err != nil {
		t.Fatal(err)
	}

	fmt.Print(resp.Objects[0])

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

}
80

Mathias Weber's avatar
Mathias Weber committed
81
82
83
84
85
86
87
88
89
90
91
92
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
func TestManyUpdates(t *testing.T) {
	client, err := NewClient(Host{"127.0.0.1", 8087})
	if err != nil {
		t.Fatal(err)
	}

	crdtType := CRDTType_COUNTER
	key := &ApbBoundObject{
		Bucket: []byte("bucket"),
		Key:    []byte("keyMany"),
		Type:   &crdtType}
	one := int64(1)

	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)
				}
				_, err = tx.Update(&ApbUpdateOp{
					Boundobject: key,
					Operation:   &ApbUpdateOperation{Counterop: &ApbCounterUpdate{Inc: &one}},
				})
				if err != nil {
					t.Fatal(err)
				}
				_, err = tx.Commit()
				if err != nil {
					t.Fatal(err)
				}
				if i%1000 == 0 {
					fmt.Println(i)
				}
			}
		}()
	}
	wg.Wait()

	resp, err := client.StaticRead(key)
	if err != nil {
		t.Fatal(err)
	}

	fmt.Print(resp.Objects.Objects[0])
}

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
func TestStatic(t *testing.T) {
	client, err := NewClient(Host{"127.0.0.1", 8087})
	if err != nil {
		t.Fatal(err)
	}

	crdtType := CRDTType_COUNTER
	key := &ApbBoundObject{
		Bucket: []byte("bucket"),
		Key:    []byte("keyStatic"),
		Type:   &crdtType}
	one := int64(1)
	_, err = client.StaticUpdate(&ApbUpdateOp{
		Boundobject: key,
		Operation:   &ApbUpdateOperation{Counterop: &ApbCounterUpdate{Inc: &one}},
	})
	if err != nil {
		t.Fatal(err)
	}
	resp, err := client.StaticRead(key)
	if err != nil {
		t.Fatal(err)
	}

	fmt.Print(resp.Objects.Objects[0])
}