-
Notifications
You must be signed in to change notification settings - Fork 272
/
memory_test.go
117 lines (103 loc) · 2.63 KB
/
memory_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
// Copyright 2013, Örjan Persson. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package logging
import (
"strconv"
"testing"
)
// TODO share more code between these tests
func MemoryRecordN(b *MemoryBackend, n int) *Record {
node := b.Head()
for i := 0; i < n; i++ {
if node == nil {
break
}
node = node.Next()
}
if node == nil {
return nil
}
return node.Record
}
func ChannelMemoryRecordN(b *ChannelMemoryBackend, n int) *Record {
b.Flush()
node := b.Head()
for i := 0; i < n; i++ {
if node == nil {
break
}
node = node.Next()
}
if node == nil {
return nil
}
return node.Record
}
func TestMemoryBackend(t *testing.T) {
backend := NewMemoryBackend(8)
SetBackend(backend)
log := MustGetLogger("test")
if nil != MemoryRecordN(backend, 0) || 0 != backend.size {
t.Errorf("memory level: %d", backend.size)
}
// Run 13 times, the resulting vector should be [5..12]
for i := 0; i < 13; i++ {
log.Infof("%d", i)
}
if 8 != backend.size {
t.Errorf("record length: %d", backend.size)
}
record := MemoryRecordN(backend, 0)
if "5" != record.Formatted(0) {
t.Errorf("unexpected start: %s", record.Formatted(0))
}
for i := 0; i < 8; i++ {
record = MemoryRecordN(backend, i)
if strconv.Itoa(i+5) != record.Formatted(0) {
t.Errorf("unexpected record: %v", record.Formatted(0))
}
}
record = MemoryRecordN(backend, 7)
if "12" != record.Formatted(0) {
t.Errorf("unexpected end: %s", record.Formatted(0))
}
record = MemoryRecordN(backend, 8)
if nil != record {
t.Errorf("unexpected eof: %s", record.Formatted(0))
}
}
func TestChannelMemoryBackend(t *testing.T) {
backend := NewChannelMemoryBackend(8)
SetBackend(backend)
log := MustGetLogger("test")
if nil != ChannelMemoryRecordN(backend, 0) || 0 != backend.size {
t.Errorf("memory level: %d", backend.size)
}
// Run 13 times, the resulting vector should be [5..12]
for i := 0; i < 13; i++ {
log.Infof("%d", i)
}
backend.Flush()
if 8 != backend.size {
t.Errorf("record length: %d", backend.size)
}
record := ChannelMemoryRecordN(backend, 0)
if "5" != record.Formatted(0) {
t.Errorf("unexpected start: %s", record.Formatted(0))
}
for i := 0; i < 8; i++ {
record = ChannelMemoryRecordN(backend, i)
if strconv.Itoa(i+5) != record.Formatted(0) {
t.Errorf("unexpected record: %v", record.Formatted(0))
}
}
record = ChannelMemoryRecordN(backend, 7)
if "12" != record.Formatted(0) {
t.Errorf("unexpected end: %s", record.Formatted(0))
}
record = ChannelMemoryRecordN(backend, 8)
if nil != record {
t.Errorf("unexpected eof: %s", record.Formatted(0))
}
}