-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsync_test.go
92 lines (85 loc) · 1.74 KB
/
sync_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
package wal
import (
"bytes"
"encoding/json"
"os"
"sync"
"testing"
"github.com/ehrktia/memcache/datastructure"
)
func Test_compare_file_size(t *testing.T) {
// create test file and write some data
// for testing
testWal := new()
fileName := testWal.WalFile()
// test file operations
if err := testWal.createFile(); err != nil {
t.Fatal(err)
}
// open file to add some data
f, err := os.OpenFile(fileName, os.O_RDWR, 0666)
if err != nil {
t.Fatal(err)
}
// add data to file
_, err = f.Write(
[]byte("this is firstline\nthis is second line\nthis is third line\n"))
if err != nil {
t.Fatal(err)
}
f.Close()
// compare pointer to read till
got, err := testWal.compareFile()
if err != nil {
t.Fatal(err)
}
// validate
if !got {
t.Fatalf("expected-%t\tgot-%t\n", true, got)
}
t.Cleanup(func() {
if err := os.Remove(fileName); err != nil {
t.Fatal(err)
}
})
}
func Test_upd_in_memory_cache(t *testing.T) {
// create test file and write some data
// for testing
testWal := new()
fileName := testWal.WalFile()
// test file operations
if err := testWal.createFile(); err != nil {
t.Fatal(err)
}
// open file to add some data
f, err := os.OpenFile(fileName, os.O_RDWR, 0666)
if err != nil {
t.Fatal(err)
}
d := &datastructure.Data{
Key: "some-key", Value: "some-value",
}
db, err := json.Marshal(&d)
if err != nil {
t.Fatal(err)
}
if _, err := testWal.Write(db); err != nil {
t.Fatal(err)
}
f.Close()
b := &bytes.Buffer{}
// initialize store to test sync
once := &sync.Once{}
datastructure.NewQueue(once, 5)
// test upd cache
if err := testWal.UpdInMemoryCache(); err != nil {
t.Fatal(err)
}
t.Cleanup(func() {
if err := os.Remove(fileName); err != nil {
t.Fatal(err)
}
b.Reset()
})
}