-
Notifications
You must be signed in to change notification settings - Fork 16
/
secondary_test.go
129 lines (114 loc) · 3.31 KB
/
secondary_test.go
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
package main
import (
"fmt"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func TestUidFromSecondaryInByes(t *testing.T) {
note := Note{UID: "abc"}
uid, err := uidFromSecondary(note, "")
assert.Nil(t, err)
assert.Equal(t, "abc", uid)
}
func TestUidFromSecondaryNotInByes(t *testing.T) {
note := Note{}
uid, err := uidFromSecondary(note, "/tmp/n.db.secondary.abc.1234567")
assert.Nil(t, err)
assert.Equal(t, "abc", uid)
}
func TestUidFromSecondaryNotInByesOrInPath(t *testing.T) {
note := Note{}
uid, err := uidFromSecondary(note, "/tmp/n.db")
assert.NotNil(t, err)
assert.Equal(t, "", uid)
}
func TestSecondaryDeleteByUID(t *testing.T) {
mock := setup(t)
defer tearDown(mock)
note := Note{
Subject: fmt.Sprintf("secondary note creation %s", time.Now()),
}
assert.Nil(t, mock.secondary.deleteByUID(note.UID))
notes := mock.secondary.list()
assert.Equal(t, 1, len(notes))
assert.True(t, notes[0].Deleted)
}
func TestSecondaryCreateNew(t *testing.T) {
mock := setup(t)
defer tearDown(mock)
note := Note{
Subject: fmt.Sprintf("secondary note creation %s", time.Now()),
}
_, err := mock.secondary.update(note)
assert.Nil(t, err)
}
func TestSecondaryList(t *testing.T) {
mock := setup(t)
defer tearDown(mock)
note := Note{
Subject: fmt.Sprintf("secondary note creation %s", time.Now()),
}
_, err := mock.secondary.update(note)
assert.Nil(t, err)
assert.Equal(t, 1, len(mock.secondary.list()))
assert.Equal(t, note.Subject, mock.secondary.list()[0].Subject)
}
func TestSecondaryUpdateExisting(t *testing.T) {
mock := setup(t)
defer tearDown(mock)
_, s, _, _ := createTestNote(mock, "")
s.Content = fmt.Sprintf("secondary note mutation %s", time.Now())
_, err := mock.secondary.update(s)
assert.Nil(t, err)
}
func TestSecondaryReadAfterWrite(t *testing.T) {
mock := setup(t)
defer tearDown(mock)
_, s, _, _ := createTestNote(mock, "")
s.Content = fmt.Sprintf("secondary read after write %s", time.Now())
_, err := mock.secondary.update(s)
assert.Nil(t, err)
note, err := mock.secondary.getNoteByUID(s.UID, "")
assert.Nil(t, err)
assert.Equal(t, s.Content, note.Content)
}
func TestPrimaryReadAfterSecondaryWrite(t *testing.T) {
mock := setup(t)
defer tearDown(mock)
_, s, _, _ := createTestNote(mock, "")
s.Content = fmt.Sprintf("secondary note mutation for listing %s", time.Now())
_, err := mock.secondary.update(s)
assert.Nil(t, err)
// Simulare the recover process the primary would be running
consumeSecondaries(mock.db, Secondary{Path: mock.db.dbFilePath()}, new(messenger))
// Read through the primmary to see if it finds the changes
p, err := db.getNoteByUID(s.UID, "")
assert.Nil(t, err)
assert.Equal(t, s.Content, p.Content)
}
func TestReloadAsNeeded(t *testing.T) {
mock := setup(t)
defer tearDown(mock)
_, note, _, _ := createTestNote(mock, "")
frontend, backend := new(messenger), new(messenger)
frontendCh := frontend.add()
backendCh := backend.add()
go reloadAsNeeded(mock.db, frontend, backend)
time.Sleep(time.Second * 3)
mock.db.update(note)
assert.Equal(t, "reload", <-frontendCh)
backend.send("stop")
<-backendCh
}
func TestSecondaryUpdate(t *testing.T) {
mock := setup(t)
defer tearDown(mock)
note := Note{
Subject: "subject",
Content: "body",
}
persisted, err := mock.secondary.update(note)
assert.Nil(t, err)
assert.Equal(t, note.Subject, persisted.Subject)
}