File: log_cache_test.go

package info (click to toggle)
golang-github-hashicorp-raft 0.0~git20160317.0.3359516-1~bpo8+1
  • links: PTS, VCS
  • area: main
  • in suites: jessie-backports
  • size: 428 kB
  • sloc: makefile: 16
file content (88 lines) | stat: -rw-r--r-- 1,584 bytes parent folder | download | duplicates (6)
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
package raft

import (
	"testing"
)

func TestLogCache(t *testing.T) {
	store := NewInmemStore()
	c, _ := NewLogCache(16, store)

	// Insert into the in-mem store
	for i := 0; i < 32; i++ {
		log := &Log{Index: uint64(i) + 1}
		store.StoreLog(log)
	}

	// Check the indexes
	if idx, _ := c.FirstIndex(); idx != 1 {
		t.Fatalf("bad: %d", idx)
	}
	if idx, _ := c.LastIndex(); idx != 32 {
		t.Fatalf("bad: %d", idx)
	}

	// Try get log with a miss
	var out Log
	err := c.GetLog(1, &out)
	if err != nil {
		t.Fatalf("err: %v", err)
	}
	if out.Index != 1 {
		t.Fatalf("bad: %#v", out)
	}

	// Store logs
	l1 := &Log{Index: 33}
	l2 := &Log{Index: 34}
	err = c.StoreLogs([]*Log{l1, l2})
	if err != nil {
		t.Fatalf("err: %v", err)
	}

	if idx, _ := c.LastIndex(); idx != 34 {
		t.Fatalf("bad: %d", idx)
	}

	// Check that it wrote-through
	err = store.GetLog(33, &out)
	if err != nil {
		t.Fatalf("err: %v", err)
	}
	err = store.GetLog(34, &out)
	if err != nil {
		t.Fatalf("err: %v", err)
	}

	// Delete in the backend
	err = store.DeleteRange(33, 34)
	if err != nil {
		t.Fatalf("err: %v", err)
	}

	// Should be in the ring buffer
	err = c.GetLog(33, &out)
	if err != nil {
		t.Fatalf("err: %v", err)
	}
	err = c.GetLog(34, &out)
	if err != nil {
		t.Fatalf("err: %v", err)
	}

	// Purge the ring buffer
	err = c.DeleteRange(33, 34)
	if err != nil {
		t.Fatalf("err: %v", err)
	}

	// Should not be in the ring buffer
	err = c.GetLog(33, &out)
	if err != ErrLogNotFound {
		t.Fatalf("err: %v", err)
	}
	err = c.GetLog(34, &out)
	if err != ErrLogNotFound {
		t.Fatalf("err: %v", err)
	}
}