File: integ_test.go

package info (click to toggle)
golang-github-hashicorp-go-memdb 0.0~git20170123.0.c01f56b-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 204 kB
  • ctags: 159
  • sloc: makefile: 5
file content (445 lines) | stat: -rw-r--r-- 9,230 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
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
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
package memdb

import (
	"reflect"
	"testing"
	"time"
)

// Test that multiple concurrent transactions are isolated from each other
func TestTxn_Isolation(t *testing.T) {
	db := testDB(t)
	txn1 := db.Txn(true)

	obj := &TestObject{
		ID:  "my-object",
		Foo: "abc",
		Qux: []string{"abc1", "abc2"},
	}
	obj2 := &TestObject{
		ID:  "my-cool-thing",
		Foo: "xyz",
		Qux: []string{"xyz1", "xyz2"},
	}
	obj3 := &TestObject{
		ID:  "my-other-cool-thing",
		Foo: "xyz",
		Qux: []string{"xyz1", "xyz2"},
	}

	err := txn1.Insert("main", obj)
	if err != nil {
		t.Fatalf("err: %v", err)
	}
	err = txn1.Insert("main", obj2)
	if err != nil {
		t.Fatalf("err: %v", err)
	}
	err = txn1.Insert("main", obj3)
	if err != nil {
		t.Fatalf("err: %v", err)
	}

	// Results should show up in this transaction
	raw, err := txn1.First("main", "id")
	if err != nil {
		t.Fatalf("err: %v", err)
	}
	if raw == nil {
		t.Fatalf("bad: %#v", raw)
	}

	// Create a new transaction, current one is NOT committed
	txn2 := db.Txn(false)

	// Nothing should show up in this transaction
	raw, err = txn2.First("main", "id")
	if err != nil {
		t.Fatalf("err: %v", err)
	}
	if raw != nil {
		t.Fatalf("bad: %#v", raw)
	}

	// Commit txn1, txn2 should still be isolated
	txn1.Commit()

	// Nothing should show up in this transaction
	raw, err = txn2.First("main", "id")
	if err != nil {
		t.Fatalf("err: %v", err)
	}
	if raw != nil {
		t.Fatalf("bad: %#v", raw)
	}

	// Create a new txn
	txn3 := db.Txn(false)

	// Results should show up in this transaction
	raw, err = txn3.First("main", "id")
	if err != nil {
		t.Fatalf("err: %v", err)
	}
	if raw == nil {
		t.Fatalf("bad: %#v", raw)
	}
}

// Test that an abort clears progress
func TestTxn_Abort(t *testing.T) {
	db := testDB(t)
	txn1 := db.Txn(true)

	obj := &TestObject{
		ID:  "my-object",
		Foo: "abc",
		Qux: []string{"abc1", "abc2"},
	}
	obj2 := &TestObject{
		ID:  "my-cool-thing",
		Foo: "xyz",
		Qux: []string{"xyz1", "xyz2"},
	}
	obj3 := &TestObject{
		ID:  "my-other-cool-thing",
		Foo: "xyz",
		Qux: []string{"xyz1", "xyz2"},
	}

	err := txn1.Insert("main", obj)
	if err != nil {
		t.Fatalf("err: %v", err)
	}
	err = txn1.Insert("main", obj2)
	if err != nil {
		t.Fatalf("err: %v", err)
	}
	err = txn1.Insert("main", obj3)
	if err != nil {
		t.Fatalf("err: %v", err)
	}

	// Abort the txn
	txn1.Abort()
	txn1.Commit()

	// Create a new transaction
	txn2 := db.Txn(false)

	// Nothing should show up in this transaction
	raw, err := txn2.First("main", "id")
	if err != nil {
		t.Fatalf("err: %v", err)
	}
	if raw != nil {
		t.Fatalf("bad: %#v", raw)
	}
}

func TestComplexDB(t *testing.T) {
	db := testComplexDB(t)
	testPopulateData(t, db)
	txn := db.Txn(false) // read only

	// Get using a full name
	raw, err := txn.First("people", "name", "Armon", "Dadgar")
	noErr(t, err)
	if raw == nil {
		t.Fatalf("should get person")
	}

	// Get using a prefix
	raw, err = txn.First("people", "name_prefix", "Armon")
	noErr(t, err)
	if raw == nil {
		t.Fatalf("should get person")
	}

	raw, err = txn.First("people", "id_prefix", raw.(*TestPerson).ID[:4])
	noErr(t, err)
	if raw == nil {
		t.Fatalf("should get person")
	}

	// Get based on field set.
	result, err := txn.Get("people", "sibling", true)
	noErr(t, err)
	if raw == nil {
		t.Fatalf("should get person")
	}

	exp := map[string]bool{"Alex": true, "Armon": true}
	act := make(map[string]bool, 2)
	for i := result.Next(); i != nil; i = result.Next() {
		p, ok := i.(*TestPerson)
		if !ok {
			t.Fatalf("should get person")
		}
		act[p.First] = true
	}

	if !reflect.DeepEqual(act, exp) {
		t.Fatalf("Got %#v; want %#v", act, exp)
	}

	raw, err = txn.First("people", "sibling", false)
	noErr(t, err)
	if raw == nil {
		t.Fatalf("should get person")
	}
	if raw.(*TestPerson).First != "Mitchell" {
		t.Fatalf("wrong person!")
	}

	// Where in the world is mitchell hashimoto?
	raw, err = txn.First("people", "name_prefix", "Mitchell")
	noErr(t, err)
	if raw == nil {
		t.Fatalf("should get person")
	}

	person := raw.(*TestPerson)
	if person.First != "Mitchell" {
		t.Fatalf("wrong person!")
	}

	raw, err = txn.First("visits", "id_prefix", person.ID)
	noErr(t, err)
	if raw == nil {
		t.Fatalf("should get visit")
	}

	visit := raw.(*TestVisit)

	raw, err = txn.First("places", "id", visit.Place)
	noErr(t, err)
	if raw == nil {
		t.Fatalf("should get place")
	}

	place := raw.(*TestPlace)
	if place.Name != "Maui" {
		t.Fatalf("bad place (but isn't anywhere else really?): %v", place)
	}
}

func TestWatchUpdate(t *testing.T) {
	db := testComplexDB(t)
	testPopulateData(t, db)
	txn := db.Txn(false) // read only

	watchSetIter := NewWatchSet()
	watchSetSpecific := NewWatchSet()
	watchSetPrefix := NewWatchSet()

	// Get using an iterator.
	iter, err := txn.Get("people", "name", "Armon", "Dadgar")
	noErr(t, err)
	watchSetIter.Add(iter.WatchCh())
	if raw := iter.Next(); raw == nil {
		t.Fatalf("should get person")
	}

	// Get using a full name.
	watch, raw, err := txn.FirstWatch("people", "name", "Armon", "Dadgar")
	noErr(t, err)
	if raw == nil {
		t.Fatalf("should get person")
	}
	watchSetSpecific.Add(watch)

	// Get using a prefix.
	watch, raw, err = txn.FirstWatch("people", "name_prefix", "Armon")
	noErr(t, err)
	if raw == nil {
		t.Fatalf("should get person")
	}
	watchSetPrefix.Add(watch)

	// Write to a snapshot.
	snap := db.Snapshot()
	txn2 := snap.Txn(true) // write
	noErr(t, txn2.Delete("people", raw))
	txn2.Commit()

	// None of the watches should trigger since we didn't alter the
	// primary.
	wait := 100 * time.Millisecond
	if timeout := watchSetIter.Watch(time.After(wait)); !timeout {
		t.Fatalf("should timeout")
	}
	if timeout := watchSetSpecific.Watch(time.After(wait)); !timeout {
		t.Fatalf("should timeout")
	}
	if timeout := watchSetPrefix.Watch(time.After(wait)); !timeout {
		t.Fatalf("should timeout")
	}

	// Write to the primary.
	txn3 := db.Txn(true) // write
	noErr(t, txn3.Delete("people", raw))
	txn3.Commit()

	// All three watches should trigger!
	wait = time.Second
	if timeout := watchSetIter.Watch(time.After(wait)); timeout {
		t.Fatalf("should not timeout")
	}
	if timeout := watchSetSpecific.Watch(time.After(wait)); timeout {
		t.Fatalf("should not timeout")
	}
	if timeout := watchSetPrefix.Watch(time.After(wait)); timeout {
		t.Fatalf("should not timeout")
	}
}

func testPopulateData(t *testing.T, db *MemDB) {
	// Start write txn
	txn := db.Txn(true)

	// Create some data
	person1 := testPerson()

	person2 := testPerson()
	person2.First = "Mitchell"
	person2.Last = "Hashimoto"

	person3 := testPerson()
	person3.First = "Alex"
	person3.Last = "Dadgar"

	person1.Sibling = person3
	person3.Sibling = person1

	place1 := testPlace()
	place2 := testPlace()
	place2.Name = "Maui"

	visit1 := &TestVisit{person1.ID, place1.ID}
	visit2 := &TestVisit{person2.ID, place2.ID}

	// Insert it all
	noErr(t, txn.Insert("people", person1))
	noErr(t, txn.Insert("people", person2))
	noErr(t, txn.Insert("people", person3))
	noErr(t, txn.Insert("places", place1))
	noErr(t, txn.Insert("places", place2))
	noErr(t, txn.Insert("visits", visit1))
	noErr(t, txn.Insert("visits", visit2))

	// Commit
	txn.Commit()
}

func noErr(t *testing.T, err error) {
	if err != nil {
		t.Fatalf("err: %v", err)
	}
}

type TestPerson struct {
	ID      string
	First   string
	Last    string
	Sibling *TestPerson
}

type TestPlace struct {
	ID   string
	Name string
}

type TestVisit struct {
	Person string
	Place  string
}

func testComplexSchema() *DBSchema {
	return &DBSchema{
		Tables: map[string]*TableSchema{
			"people": &TableSchema{
				Name: "people",
				Indexes: map[string]*IndexSchema{
					"id": &IndexSchema{
						Name:    "id",
						Unique:  true,
						Indexer: &UUIDFieldIndex{Field: "ID"},
					},
					"name": &IndexSchema{
						Name:   "name",
						Unique: true,
						Indexer: &CompoundIndex{
							Indexes: []Indexer{
								&StringFieldIndex{Field: "First"},
								&StringFieldIndex{Field: "Last"},
							},
						},
					},
					"sibling": &IndexSchema{
						Name:    "sibling",
						Unique:  false,
						Indexer: &FieldSetIndex{Field: "Sibling"},
					},
				},
			},
			"places": &TableSchema{
				Name: "places",
				Indexes: map[string]*IndexSchema{
					"id": &IndexSchema{
						Name:    "id",
						Unique:  true,
						Indexer: &UUIDFieldIndex{Field: "ID"},
					},
					"name": &IndexSchema{
						Name:    "name",
						Unique:  true,
						Indexer: &StringFieldIndex{Field: "Name"},
					},
				},
			},
			"visits": &TableSchema{
				Name: "visits",
				Indexes: map[string]*IndexSchema{
					"id": &IndexSchema{
						Name:   "id",
						Unique: true,
						Indexer: &CompoundIndex{
							Indexes: []Indexer{
								&UUIDFieldIndex{Field: "Person"},
								&UUIDFieldIndex{Field: "Place"},
							},
						},
					},
				},
			},
		},
	}
}

func testComplexDB(t *testing.T) *MemDB {
	db, err := NewMemDB(testComplexSchema())
	if err != nil {
		t.Fatalf("err: %v", err)
	}
	return db
}

func testPerson() *TestPerson {
	_, uuid := generateUUID()
	obj := &TestPerson{
		ID:    uuid,
		First: "Armon",
		Last:  "Dadgar",
	}
	return obj
}

func testPlace() *TestPlace {
	_, uuid := generateUUID()
	obj := &TestPlace{
		ID:   uuid,
		Name: "HashiCorp",
	}
	return obj
}