forked from nikoksr/notify
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsyslog_test.go
93 lines (74 loc) · 1.96 KB
/
syslog_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
package syslog
import (
"context"
"log/syslog"
"testing"
"github.com/pkg/errors"
"github.com/stretchr/testify/require"
)
func TestSyslog_New(t *testing.T) {
t.Parallel()
assert := require.New(t)
// Test creating a local writer with invalid log priority.
svc, err := New(-1, "")
assert.Error(err)
assert.Nil(svc)
// Test creating a local writer successfully.
svc, err = New(syslog.LOG_USER, "")
assert.NoError(err)
assert.NotNil(svc)
err = svc.Close()
assert.NoError(err)
// Test creating a remote writer with invalid port.
svc, err = NewFromDial("tcp", "localhost:99999", syslog.LOG_USER, "")
assert.Error(err)
assert.Nil(svc)
// Test creating a remote writer successfully.
svc, err = NewFromDial("", "", syslog.LOG_USER, "")
assert.NoError(err)
assert.NotNil(svc)
err = svc.Close()
assert.NoError(err)
}
func TestSyslog_Send(t *testing.T) {
t.Parallel()
assert := require.New(t)
ctx := context.Background()
svc := new(Service)
// Test syslog writer returning error
mockClient := new(mockSyslogWriter)
mockClient.
On("Write", []byte("test: message")).
Return(0, errors.New("some error"))
svc.writer = mockClient
err := svc.Send(ctx, "test", "message")
assert.Error(err)
mockClient.AssertExpectations(t)
// Test syslog writer returning no error
mockClient = new(mockSyslogWriter)
mockClient.
On("Write", []byte("test: message")).
Return(0, nil)
svc.writer = mockClient
err = svc.Send(ctx, "test", "message")
assert.NoError(err)
mockClient.AssertExpectations(t)
// Test closing the syslog writer with error
mockClient = new(mockSyslogWriter)
mockClient.
On("Close").
Return(errors.New("some error"))
svc.writer = mockClient
err = svc.Close()
assert.Error(err)
mockClient.AssertExpectations(t)
// Test closing the syslog writer without error
mockClient = new(mockSyslogWriter)
mockClient.
On("Close").
Return(nil)
svc.writer = mockClient
err = svc.Close()
assert.NoError(err)
mockClient.AssertExpectations(t)
}