forked from fl00r/go-tarantool-1.6
-
Notifications
You must be signed in to change notification settings - Fork 63
Expand file tree
/
Copy pathlog_test.go
More file actions
194 lines (156 loc) · 4.55 KB
/
log_test.go
File metadata and controls
194 lines (156 loc) · 4.55 KB
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
package tarantool_test
import (
"bytes"
"context"
"log/slog"
"strings"
"sync"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
. "github.com/tarantool/go-tarantool/v3"
"github.com/tarantool/go-tarantool/v3/test_helpers"
)
type safeBuffer struct {
mu sync.Mutex
buf bytes.Buffer
}
func (b *safeBuffer) Write(p []byte) (int, error) {
b.mu.Lock()
defer b.mu.Unlock()
return b.buf.Write(p)
}
func (b *safeBuffer) String() string {
b.mu.Lock()
defer b.mu.Unlock()
return b.buf.String()
}
func TestLogMessageConstants(t *testing.T) {
msgs := []string{
LogMsgReconnectFailed,
LogMsgLastReconnectFailed,
LogMsgUnexpectedRequestId,
LogMsgWatchEventReadFailed,
LogMsgPushUnsupported,
}
for _, msg := range msgs {
assert.NotEmpty(t, msg, "log message constant must not be empty")
}
}
func TestLogKeyConstants(t *testing.T) {
keys := []string{
LogKeyAttempt,
LogKeyMaxAttempts,
LogKeyError,
LogKeyRequestId,
LogKeyAddress,
}
for _, key := range keys {
assert.NotEmpty(t, key, "log key constant must not be empty")
}
}
func TestConnectionCustomLogger(t *testing.T) {
var buf safeBuffer
logger := slog.New(slog.NewTextHandler(&buf, &slog.HandlerOptions{Level: slog.LevelWarn}))
testOpts := opts
testOpts.Logger = logger
conn := test_helpers.ConnectWithValidation(t, dialer, testOpts)
defer func() { _ = conn.Close() }()
_, err := conn.Do(NewCallRequest("push_func").Args([]any{1})).Get()
require.NoError(t, err)
output := buf.String()
require.Contains(t, output, LogMsgPushUnsupported)
require.Contains(t, output, LogKeyRequestId)
}
func TestConnectionLoggerWithGroup(t *testing.T) {
var buf safeBuffer
logger := slog.New(slog.NewTextHandler(&buf, &slog.HandlerOptions{Level: slog.LevelWarn}))
grouped := logger.WithGroup("myapp")
testOpts := opts
testOpts.Logger = grouped
conn := test_helpers.ConnectWithValidation(t, dialer, testOpts)
defer func() { _ = conn.Close() }()
_, err := conn.Do(NewCallRequest("push_func").Args([]any{1})).Get()
require.NoError(t, err)
output := buf.String()
require.Contains(t, output, "myapp.tarantool")
require.Contains(t, output, LogMsgPushUnsupported)
}
func TestConnectionNilLoggerDiscards(t *testing.T) {
testOpts := opts
testOpts.Logger = nil
conn := test_helpers.ConnectWithValidation(t, dialer, testOpts)
defer func() { _ = conn.Close() }()
_, err := conn.Do(NewCallRequest("push_func").Args([]any{1})).Get()
require.NoError(t, err)
}
func TestConnectionUnexpectedRequestId(t *testing.T) {
var buf safeBuffer
logger := slog.New(slog.NewTextHandler(&buf,
&slog.HandlerOptions{Level: slog.LevelWarn}))
dialer := mockIoDialer{
init: func(conn *mockIoConn) {
conn.read = make(chan struct{})
conn.written = make(chan struct{})
conn.writeWgDelay = 1
conn.readWgDelay = 2
conn.readbuf.Write([]byte{
0xce, 0x00, 0x00, 0x00, 0x0a,
0x82,
0x00, 0x00,
0x01, 0xce, 0x00, 0x00, 0xde, 0xad,
0x80,
})
conn.wgDoneOnClose = false
},
}
ctx, cancel := test_helpers.GetConnectContext()
defer cancel()
conn, err := Connect(ctx, &dialer, Opts{
Timeout: 1000 * time.Second,
SkipSchema: true,
Logger: logger,
})
require.NoError(t, err)
conn.Do(NewPingRequest())
<-dialer.conn.written
dialer.conn.written = nil
<-dialer.conn.read
<-dialer.conn.read
require.Eventually(t, func() bool {
return strings.Contains(buf.String(), LogMsgUnexpectedRequestId)
}, 2*time.Second, 50*time.Millisecond)
output := buf.String()
require.Contains(t, output, LogKeyRequestId)
dialer.conn.writeWg.Done()
_ = conn.Close()
}
func TestConnectionErrorAttributeFormat(t *testing.T) {
var buf safeBuffer
logger := slog.New(slog.NewTextHandler(&buf, &slog.HandlerOptions{Level: slog.LevelWarn}))
reconnectOpts := Opts{
Timeout: 500 * time.Millisecond,
Reconnect: 100 * time.Millisecond,
MaxReconnects: 1,
Logger: logger,
SkipSchema: true,
}
_, err := Connect(context.Background(), NetDialer{
Address: "127.0.0.1:0",
}, reconnectOpts)
require.Error(t, err)
require.Eventually(t, func() bool {
return strings.Contains(buf.String(), LogMsgReconnectFailed)
}, 3*time.Second, 50*time.Millisecond)
output := buf.String()
assert.Contains(t, output, LogKeyError)
assert.Contains(t, output, LogKeyAttempt)
assert.Contains(t, output, LogKeyMaxAttempts)
require.Eventually(t, func() bool {
return strings.Contains(buf.String(), LogMsgLastReconnectFailed)
}, 3*time.Second, 50*time.Millisecond)
output = buf.String()
assert.Contains(t, output, LogKeyError)
assert.Contains(t, output, LogKeyAddress)
}