@@ -17,6 +17,7 @@ func TestPut(t *testing.T) {
1717 }
1818
1919 b := newBackend ()
20+ defer b .testableCleanupResource ()
2021 for i , tt := range tests {
2122 b .Put (tt .rev , tt .path , tt .data )
2223 v := b .Get (tt .rev , tt .path )
@@ -27,6 +28,7 @@ func TestPut(t *testing.T) {
2728 t .Errorf ("#%d: data = %s, want %s" , i , v .data , tt .data )
2829 }
2930 }
31+
3032}
3133
3234func TestPutOnExistingPath (t * testing.T ) {
@@ -40,6 +42,7 @@ func TestPutOnExistingPath(t *testing.T) {
4042 }
4143
4244 b := newBackend ()
45+ defer b .testableCleanupResource ()
4346 for i , tt := range tests {
4447 b .Put (2 * i + 1 , tt .path , tt .data1 )
4548 v := b .Get (2 * i + 1 , tt .path )
@@ -65,6 +68,8 @@ func TestPutOnExistingPath(t *testing.T) {
6568
6669func TestGetMVCC (t * testing.T ) {
6770 b := newBackend ()
71+ defer b .testableCleanupResource ()
72+
6873 b .Put (1 , * newPath ("/a" ), []byte ("1" ))
6974 b .Put (2 , * newPath ("/b" ), []byte ("2" ))
7075 b .Put (3 , * newPath ("/a" ), []byte ("3" ))
@@ -99,12 +104,15 @@ func TestGetMVCC(t *testing.T) {
99104}
100105
101106func TestLs (t * testing.T ) {
102- back := newBackend ()
103107 d := []byte ("somedata" )
104- back .Put (1 , * newPath ("/a" ), d )
105- back .Put (2 , * newPath ("/a/b" ), d )
106- back .Put (3 , * newPath ("/a/c" ), d )
107- back .Put (4 , * newPath ("/b" ), d )
108+
109+ b := newBackend ()
110+ defer b .testableCleanupResource ()
111+
112+ b .Put (1 , * newPath ("/a" ), d )
113+ b .Put (2 , * newPath ("/a/b" ), d )
114+ b .Put (3 , * newPath ("/a/c" ), d )
115+ b .Put (4 , * newPath ("/b" ), d )
108116
109117 tests := []struct {
110118 p string
@@ -118,7 +126,7 @@ func TestLs(t *testing.T) {
118126 {"/c" , []string {}},
119127 }
120128 for i , tt := range tests {
121- ps := back .Ls (tt .p )
129+ ps := b .Ls (tt .p )
122130 if len (ps ) != len (tt .wps ) {
123131 t .Fatalf ("#%d: len(ps) = %d, want %d" , i , len (ps ), len (tt .wps ))
124132 }
@@ -130,9 +138,44 @@ func TestLs(t *testing.T) {
130138 }
131139}
132140
141+ func TestRestore (t * testing.T ) {
142+ tests := []struct {
143+ rev int
144+ path Path
145+ data []byte
146+ }{
147+ {1 , * newPath ("/foo/bar" ), []byte ("somedata" )},
148+ {2 , * newPath ("/bar/foo" ), []byte ("datasome" )},
149+ }
150+
151+ b := newBackend ()
152+ for _ , tt := range tests {
153+ // append records to the log
154+ b .Put (tt .rev , tt .path , tt .data )
155+ }
156+ b .dblog .Close ()
157+
158+ // simulate restoring log in another backend
159+ b2 , err := newBackendWithConfig (b .config )
160+ defer b2 .testableCleanupResource ()
161+ if err != nil {
162+ t .Errorf ("newBackendWithConfig failed: %v" , err )
163+ }
164+ for i , tt := range tests {
165+ v := b2 .Get (tt .rev , tt .path )
166+ if v .rev != tt .rev {
167+ t .Errorf ("#%d: rev = %d, want %d" , i , v .rev , tt .rev )
168+ }
169+ if ! reflect .DeepEqual (v .data , tt .data ) {
170+ t .Errorf ("#%d: data = %s, want %s" , i , v .data , tt .data )
171+ }
172+ }
173+ }
174+
133175func BenchmarkPut (b * testing.B ) {
134176 b .StopTimer ()
135177 back := newBackend ()
178+ defer back .testableCleanupResource ()
136179 d := []byte ("somedata" )
137180 path := make ([]Path , b .N )
138181 for i := range path {
@@ -148,6 +191,8 @@ func BenchmarkPut(b *testing.B) {
148191func BenchmarkGetWithCache (b * testing.B ) {
149192 b .StopTimer ()
150193 back := newBackend ()
194+ defer back .testableCleanupResource ()
195+
151196 d := []byte ("somedata" )
152197 path := make ([]Path , b .N )
153198 for i := range path {
@@ -168,6 +213,7 @@ func BenchmarkGetWithCache(b *testing.B) {
168213func BenchmarkGetWithOutCache (b * testing.B ) {
169214 b .StopTimer ()
170215 back := newBackend ()
216+ defer back .testableCleanupResource ()
171217 back .cache = nil
172218 d := []byte ("somedata" )
173219 path := make ([]Path , b .N )
0 commit comments