File: group_test.go

package info (click to toggle)
tendermint-go-autofile 0.0~20170129~0git48b17de-1
  • links: PTS, VCS
  • area: main
  • in suites: buster, sid
  • size: 108 kB
  • ctags: 74
  • sloc: sh: 23; makefile: 4
file content (405 lines) | stat: -rw-r--r-- 9,150 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
package autofile

import (
	"errors"
	"io"
	"io/ioutil"
	"os"
	"strconv"
	"strings"
	"testing"

	. "github.com/tendermint/go-common"
)

// NOTE: Returned group has ticker stopped
func createTestGroup(t *testing.T, headSizeLimit int64) *Group {
	testID := RandStr(12)
	testDir := "_test_" + testID
	err := EnsureDir(testDir, 0700)
	if err != nil {
		t.Fatal("Error creating dir", err)
	}
	headPath := testDir + "/myfile"
	g, err := OpenGroup(headPath)
	if err != nil {
		t.Fatal("Error opening Group", err)
	}
	g.SetHeadSizeLimit(headSizeLimit)
	g.stopTicker()

	if g == nil {
		t.Fatal("Failed to create Group")
	}
	return g
}

func destroyTestGroup(t *testing.T, g *Group) {
	err := os.RemoveAll(g.Dir)
	if err != nil {
		t.Fatal("Error removing test Group directory", err)
	}
}

func assertGroupInfo(t *testing.T, gInfo GroupInfo, minIndex, maxIndex int, totalSize, headSize int64) {
	if gInfo.MinIndex != minIndex {
		t.Errorf("GroupInfo MinIndex expected %v, got %v", minIndex, gInfo.MinIndex)
	}
	if gInfo.MaxIndex != maxIndex {
		t.Errorf("GroupInfo MaxIndex expected %v, got %v", maxIndex, gInfo.MaxIndex)
	}
	if gInfo.TotalSize != totalSize {
		t.Errorf("GroupInfo TotalSize expected %v, got %v", totalSize, gInfo.TotalSize)
	}
	if gInfo.HeadSize != headSize {
		t.Errorf("GroupInfo HeadSize expected %v, got %v", headSize, gInfo.HeadSize)
	}
}

func TestCheckHeadSizeLimit(t *testing.T) {
	g := createTestGroup(t, 1000*1000)

	// At first, there are no files.
	assertGroupInfo(t, g.ReadGroupInfo(), 0, 0, 0, 0)

	// Write 1000 bytes 999 times.
	for i := 0; i < 999; i++ {
		err := g.WriteLine(RandStr(999))
		if err != nil {
			t.Fatal("Error appending to head", err)
		}
	}
	g.Flush()
	assertGroupInfo(t, g.ReadGroupInfo(), 0, 0, 999000, 999000)

	// Even calling checkHeadSizeLimit manually won't rotate it.
	g.checkHeadSizeLimit()
	assertGroupInfo(t, g.ReadGroupInfo(), 0, 0, 999000, 999000)

	// Write 1000 more bytes.
	err := g.WriteLine(RandStr(999))
	if err != nil {
		t.Fatal("Error appending to head", err)
	}
	g.Flush()

	// Calling checkHeadSizeLimit this time rolls it.
	g.checkHeadSizeLimit()
	assertGroupInfo(t, g.ReadGroupInfo(), 0, 1, 1000000, 0)

	// Write 1000 more bytes.
	err = g.WriteLine(RandStr(999))
	if err != nil {
		t.Fatal("Error appending to head", err)
	}
	g.Flush()

	// Calling checkHeadSizeLimit does nothing.
	g.checkHeadSizeLimit()
	assertGroupInfo(t, g.ReadGroupInfo(), 0, 1, 1001000, 1000)

	// Write 1000 bytes 999 times.
	for i := 0; i < 999; i++ {
		err := g.WriteLine(RandStr(999))
		if err != nil {
			t.Fatal("Error appending to head", err)
		}
	}
	g.Flush()
	assertGroupInfo(t, g.ReadGroupInfo(), 0, 1, 2000000, 1000000)

	// Calling checkHeadSizeLimit rolls it again.
	g.checkHeadSizeLimit()
	assertGroupInfo(t, g.ReadGroupInfo(), 0, 2, 2000000, 0)

	// Write 1000 more bytes.
	_, err = g.Head.Write([]byte(RandStr(999) + "\n"))
	if err != nil {
		t.Fatal("Error appending to head", err)
	}
	g.Flush()
	assertGroupInfo(t, g.ReadGroupInfo(), 0, 2, 2001000, 1000)

	// Calling checkHeadSizeLimit does nothing.
	g.checkHeadSizeLimit()
	assertGroupInfo(t, g.ReadGroupInfo(), 0, 2, 2001000, 1000)

	// Cleanup
	destroyTestGroup(t, g)
}

func TestSearch(t *testing.T) {
	g := createTestGroup(t, 10*1000)

	// Create some files in the group that have several INFO lines in them.
	// Try to put the INFO lines in various spots.
	for i := 0; i < 100; i++ {
		// The random junk at the end ensures that this INFO linen
		// is equally likely to show up at the end.
		_, err := g.Head.Write([]byte(Fmt("INFO %v %v\n", i, RandStr(123))))
		if err != nil {
			t.Error("Failed to write to head")
		}
		g.checkHeadSizeLimit()
		for j := 0; j < 10; j++ {
			_, err := g.Head.Write([]byte(RandStr(123) + "\n"))
			if err != nil {
				t.Error("Failed to write to head")
			}
			g.checkHeadSizeLimit()
		}
	}

	// Create a search func that searches for line
	makeSearchFunc := func(target int) SearchFunc {
		return func(line string) (int, error) {
			parts := strings.Split(line, " ")
			if len(parts) != 3 {
				return -1, errors.New("Line did not have 3 parts")
			}
			i, err := strconv.Atoi(parts[1])
			if err != nil {
				return -1, errors.New("Failed to parse INFO: " + err.Error())
			}
			if target < i {
				return 1, nil
			} else if target == i {
				return 0, nil
			} else {
				return -1, nil
			}
		}
	}

	// Now search for each number
	for i := 0; i < 100; i++ {
		t.Log("Testing for i", i)
		gr, match, err := g.Search("INFO", makeSearchFunc(i))
		if err != nil {
			t.Fatal("Failed to search for line:", err)
		}
		if !match {
			t.Error("Expected Search to return exact match")
		}
		line, err := gr.ReadLine()
		if err != nil {
			t.Fatal("Failed to read line after search", err)
		}
		if !strings.HasPrefix(line, Fmt("INFO %v ", i)) {
			t.Fatal("Failed to get correct line")
		}
		// Make sure we can continue to read from there.
		cur := i + 1
		for {
			line, err := gr.ReadLine()
			if err == io.EOF {
				if cur == 99+1 {
					// OK!
					break
				} else {
					t.Fatal("Got EOF after the wrong INFO #")
				}
			} else if err != nil {
				t.Fatal("Error reading line", err)
			}
			if !strings.HasPrefix(line, "INFO ") {
				continue
			}
			if !strings.HasPrefix(line, Fmt("INFO %v ", cur)) {
				t.Fatalf("Unexpected INFO #. Expected %v got:\n%v", cur, line)
			}
			cur += 1
		}
		gr.Close()
	}

	// Now search for something that is too small.
	// We should get the first available line.
	{
		gr, match, err := g.Search("INFO", makeSearchFunc(-999))
		if err != nil {
			t.Fatal("Failed to search for line:", err)
		}
		if match {
			t.Error("Expected Search to not return exact match")
		}
		line, err := gr.ReadLine()
		if err != nil {
			t.Fatal("Failed to read line after search", err)
		}
		if !strings.HasPrefix(line, "INFO 0 ") {
			t.Error("Failed to fetch correct line, which is the earliest INFO")
		}
		err = gr.Close()
		if err != nil {
			t.Error("Failed to close GroupReader", err)
		}
	}

	// Now search for something that is too large.
	// We should get an EOF error.
	{
		gr, _, err := g.Search("INFO", makeSearchFunc(999))
		if err != io.EOF {
			t.Error("Expected to get an EOF error")
		}
		if gr != nil {
			t.Error("Expected to get nil GroupReader")
		}
	}

	// Cleanup
	destroyTestGroup(t, g)
}

func TestRotateFile(t *testing.T) {
	g := createTestGroup(t, 0)
	g.WriteLine("Line 1")
	g.WriteLine("Line 2")
	g.WriteLine("Line 3")
	g.Flush()
	g.RotateFile()
	g.WriteLine("Line 4")
	g.WriteLine("Line 5")
	g.WriteLine("Line 6")
	g.Flush()

	// Read g.Head.Path+"000"
	body1, err := ioutil.ReadFile(g.Head.Path + ".000")
	if err != nil {
		t.Error("Failed to read first rolled file")
	}
	if string(body1) != "Line 1\nLine 2\nLine 3\n" {
		t.Errorf("Got unexpected contents: [%v]", string(body1))
	}

	// Read g.Head.Path
	body2, err := ioutil.ReadFile(g.Head.Path)
	if err != nil {
		t.Error("Failed to read first rolled file")
	}
	if string(body2) != "Line 4\nLine 5\nLine 6\n" {
		t.Errorf("Got unexpected contents: [%v]", string(body2))
	}

	// Cleanup
	destroyTestGroup(t, g)
}

func TestFindLast1(t *testing.T) {
	g := createTestGroup(t, 0)

	g.WriteLine("Line 1")
	g.WriteLine("Line 2")
	g.WriteLine("# a")
	g.WriteLine("Line 3")
	g.Flush()
	g.RotateFile()
	g.WriteLine("Line 4")
	g.WriteLine("Line 5")
	g.WriteLine("Line 6")
	g.WriteLine("# b")
	g.Flush()

	match, found, err := g.FindLast("#")
	if err != nil {
		t.Error("Unexpected error", err)
	}
	if !found {
		t.Error("Expected found=True")
	}
	if match != "# b" {
		t.Errorf("Unexpected match: [%v]", match)
	}

	// Cleanup
	destroyTestGroup(t, g)
}

func TestFindLast2(t *testing.T) {
	g := createTestGroup(t, 0)

	g.WriteLine("Line 1")
	g.WriteLine("Line 2")
	g.WriteLine("Line 3")
	g.Flush()
	g.RotateFile()
	g.WriteLine("# a")
	g.WriteLine("Line 4")
	g.WriteLine("Line 5")
	g.WriteLine("# b")
	g.WriteLine("Line 6")
	g.Flush()

	match, found, err := g.FindLast("#")
	if err != nil {
		t.Error("Unexpected error", err)
	}
	if !found {
		t.Error("Expected found=True")
	}
	if match != "# b" {
		t.Errorf("Unexpected match: [%v]", match)
	}

	// Cleanup
	destroyTestGroup(t, g)
}

func TestFindLast3(t *testing.T) {
	g := createTestGroup(t, 0)

	g.WriteLine("Line 1")
	g.WriteLine("# a")
	g.WriteLine("Line 2")
	g.WriteLine("# b")
	g.WriteLine("Line 3")
	g.Flush()
	g.RotateFile()
	g.WriteLine("Line 4")
	g.WriteLine("Line 5")
	g.WriteLine("Line 6")
	g.Flush()

	match, found, err := g.FindLast("#")
	if err != nil {
		t.Error("Unexpected error", err)
	}
	if !found {
		t.Error("Expected found=True")
	}
	if match != "# b" {
		t.Errorf("Unexpected match: [%v]", match)
	}

	// Cleanup
	destroyTestGroup(t, g)
}

func TestFindLast4(t *testing.T) {
	g := createTestGroup(t, 0)

	g.WriteLine("Line 1")
	g.WriteLine("Line 2")
	g.WriteLine("Line 3")
	g.Flush()
	g.RotateFile()
	g.WriteLine("Line 4")
	g.WriteLine("Line 5")
	g.WriteLine("Line 6")
	g.Flush()

	match, found, err := g.FindLast("#")
	if err != nil {
		t.Error("Unexpected error", err)
	}
	if found {
		t.Error("Expected found=False")
	}
	if match != "" {
		t.Errorf("Unexpected match: [%v]", match)
	}

	// Cleanup
	destroyTestGroup(t, g)
}