Skip to content

Commit b53eb85

Browse files
authored
[type: test]Add some test methods for files which in package log (#97)
1 parent a1e25e1 commit b53eb85

File tree

2 files changed

+193
-0
lines changed

2 files changed

+193
-0
lines changed

log/async_bufwriter_test.go

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
/*
2+
* Copyright (c) 2022, AcmeStack
3+
* All rights reserved.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package log
19+
20+
import (
21+
"bytes"
22+
"io"
23+
"reflect"
24+
"testing"
25+
"time"
26+
)
27+
28+
// TestNewAsyncBufferWriter test NewAsyncBufferWriter method
29+
func TestNewAsyncBufferWriter(t *testing.T) {
30+
tests := []struct {
31+
writer io.Writer
32+
config Config
33+
}{
34+
{&bytes.Buffer{}, Config{
35+
FlushSize: 10240,
36+
BufferSize: 10240,
37+
FlushInterval: time.Second,
38+
Block: true,
39+
}},
40+
}
41+
42+
for _, tt := range tests {
43+
asyncBufferLogWriter := NewAsyncBufferWriter(tt.writer, tt.config)
44+
if reflect.TypeOf(asyncBufferLogWriter) != reflect.TypeOf(&AsyncBufferLogWriter{}) {
45+
t.Errorf("NewAsyncBufferWriter() method error, got = %v, want %v", reflect.TypeOf(asyncBufferLogWriter), reflect.TypeOf(&AsyncBufferLogWriter{}))
46+
}
47+
}
48+
}
49+
50+
// TestAsyncBufferLogWriter_Write test AsyncBufferLogWriter.Write method
51+
func TestAsyncBufferLogWriter_Write(t *testing.T) {
52+
type want struct {
53+
n int
54+
err error
55+
}
56+
tests := []struct {
57+
asyncWriter *AsyncBufferLogWriter
58+
data []byte
59+
wt *want
60+
}{
61+
{
62+
NewAsyncBufferWriter(&bytes.Buffer{}),
63+
[]byte("hello"),
64+
&want{
65+
n: 5,
66+
err: nil,
67+
},
68+
},
69+
}
70+
71+
for _, tt := range tests {
72+
n, er := tt.asyncWriter.Write(tt.data)
73+
if n != tt.wt.n || er != tt.wt.err {
74+
t.Errorf("AsyncBufferLogWriter Write() method error ! got: n=%v, error=%v want: n=%v, error=%v", n, er, tt.wt.n, tt.wt.err)
75+
}
76+
}
77+
}
78+
79+
// TestAsyncBufferLogWriter_Close test AsyncBufferLogWriter.Close method
80+
func TestAsyncBufferLogWriter_Close(t *testing.T) {
81+
tests := []struct {
82+
asyncBufferWriter *AsyncBufferLogWriter
83+
want error
84+
}{
85+
{
86+
NewAsyncBufferWriter(&bytes.Buffer{}),
87+
nil,
88+
},
89+
}
90+
91+
for _, tt := range tests {
92+
error := tt.asyncBufferWriter.Close()
93+
if error != tt.want {
94+
t.Errorf("AsyncBufferLogWriter Close() method error ! got=%v, want=%v", error, tt.want)
95+
}
96+
}
97+
}

log/async_writer_test.go

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
/*
2+
* Copyright (c) 2022, AcmeStack
3+
* All rights reserved.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package log
19+
20+
import (
21+
"bytes"
22+
"io"
23+
"reflect"
24+
"testing"
25+
)
26+
27+
// TestNewAsyncWriter test NewAsyncWriter method
28+
// @params t tests params
29+
func TestNewAsyncWriter(t *testing.T) {
30+
tests := []struct {
31+
writer io.Writer
32+
bufSize int
33+
block bool
34+
}{
35+
{&bytes.Buffer{}, 0, false},
36+
{&bytes.Buffer{}, 1024, true},
37+
}
38+
39+
for _, tt := range tests {
40+
asyncLogWriter := NewAsyncWriter(tt.writer, tt.bufSize, tt.block)
41+
if reflect.TypeOf(asyncLogWriter) != reflect.TypeOf(&AsyncLogWriter{}) {
42+
t.Errorf("NewAsyncWriter() method error, got = %v, want %v", reflect.TypeOf(asyncLogWriter), reflect.TypeOf(&AsyncLogWriter{}))
43+
}
44+
}
45+
}
46+
47+
// TestAsyncLogWriter_Write test AsyncLogWriter.Write method
48+
// @params t tests params
49+
func TestAsyncLogWriter_Write(t *testing.T) {
50+
type want struct {
51+
n int
52+
err error
53+
}
54+
tests := []struct {
55+
asyncWriter *AsyncLogWriter
56+
data []byte
57+
wt *want
58+
}{
59+
{
60+
NewAsyncWriter(&bytes.Buffer{}, 0, false),
61+
[]byte("hello"),
62+
&want{
63+
n: 5,
64+
err: nil,
65+
},
66+
},
67+
}
68+
69+
for _, tt := range tests {
70+
n, er := tt.asyncWriter.Write(tt.data)
71+
if n != tt.wt.n || er != tt.wt.err {
72+
t.Errorf("AsyncLogWriter Write() method error ! got: n=%v, error=%v want: n=%v, error=%v", n, er, tt.wt.n, tt.wt.err)
73+
}
74+
}
75+
76+
}
77+
78+
// TestAsyncLogWriter_Close test AsyncLogWriter.Close method
79+
func TestAsyncLogWriter_Close(t *testing.T) {
80+
tests := []struct {
81+
asyncWriter *AsyncLogWriter
82+
want error
83+
}{
84+
{
85+
NewAsyncWriter(&bytes.Buffer{}, 0, false),
86+
nil,
87+
},
88+
}
89+
90+
for _, tt := range tests {
91+
error := tt.asyncWriter.Close()
92+
if error != tt.want {
93+
t.Errorf("AsyncLogWriter Close() method error ! got=%v, want=%v", error, tt.want)
94+
}
95+
}
96+
}

0 commit comments

Comments
 (0)