-
Notifications
You must be signed in to change notification settings - Fork 1
/
burst_test.go
158 lines (125 loc) · 3.22 KB
/
burst_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
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
package syslog5424
import (
"log"
"os"
"sync"
"testing"
"time"
)
const burstSocket string = "./test-burst.socket"
const burstMessage string = "doing some stuff"
const burstPacket string = "<27>1 2014-12-20T14:04:00Z localhost client-app 1234 - - ERR : doing some stuff"
const burstCount int = 100000
type burstTestOk struct {
sock string
network string
transport Transport
}
//*
func TestBurst(t *testing.T) {
seq := []burstTestOk{
{"u5425", "unix", TransportRFC5425},
{"ulf", "unix", TransportLFEnded},
{"uzero", "unix", TransportZeroEnded},
{"dlf", "unixgram", TransportLFEnded},
{"dzero", "unixgram", TransportZeroEnded},
{"d5425", "unixgram", TransportRFC5425},
}
for _, s := range seq {
t.Logf("burst on [%s] [%s]", s.network, s.transport.String())
err := burst(burstSocket+s.sock, s.network, s.transport)
if err != nil {
t.Logf("[%s] [%s] %v", s.network, s.transport.String(), err)
t.Fail()
}
}
}
//*/
func burst(sock, n string, t Transport) (err error) {
defer os.Remove(sock)
os.Remove(sock)
err = nil
Now = func() time.Time {
t, _ := time.ParseInLocation("2006-01-02T15:04:00Z", "2014-12-20T14:04:00Z", time.UTC)
return t
}
wg := new(sync.WaitGroup)
mutex := new(sync.Mutex)
mutex.Lock()
wg.Add(2)
go serverBurst(wg, mutex, sock, n, t, burstCount)
go clientBurst(wg, mutex, sock, n, t, burstCount+100)
wg.Wait()
mutex.Unlock()
return
}
func clientBurst(wg *sync.WaitGroup, mutex *sync.Mutex, sock, n string, t Transport, count int) {
defer wg.Done()
// waiting the creation of the socket
mutex.Lock()
slConn, chanErr, err := (Dialer{
FlushDelay: 100 * time.Millisecond,
}).Dial(n, sock, t)
if err != nil {
log.Fatalf("client Dial %q", err)
}
defer slConn.End()
go func() {
if err := <-chanErr; err != nil {
log.Fatalf("client chanErr %q", err)
}
}()
syslog, err := New(slConn, LogDAEMON|LogWARNING, "client-app")
if err != nil {
log.Fatalf("client New %q", err)
}
syslog.TestMode()
loggerErrorConf := syslog.Channel(LogERR).Logger("ERR : ")
for i := 0; i < count; i++ {
loggerErrorConf.Print(burstMessage)
}
}
func serverBurst(wg *sync.WaitGroup, mutex *sync.Mutex, sock, n string, t Transport, count int) {
defer wg.Done()
listener, err := GuessListener(n, sock)
if err != nil {
log.Fatalf("server Collect %q", err)
}
collect, chanErr := NewReceiver(listener, 100, t)
defer collect.End()
go func() {
if err := <-chanErr; err != nil {
log.Fatalf("client chanErr %q", err)
}
}()
// socket is created
mutex.Unlock()
for i := 0; i < count; i++ {
msg, err, _ := collect.Receive()
if err != nil {
log.Fatalf("server receive %q", err)
}
if msg.String() != burstPacket {
log.Fatalf("server got %q expected %q", msg, burstPacket)
}
}
}
func BenchmarkBurst(b *testing.B) {
sock := burstSocket + "-bench"
defer os.Remove(sock)
os.Remove(sock)
wg := new(sync.WaitGroup)
mutex := new(sync.Mutex)
Now = func() time.Time {
t, _ := time.ParseInLocation("2006-01-02T15:04:00Z", "2014-12-20T14:04:00Z", time.UTC)
return t
}
mutex.Lock()
wg.Add(2)
go clientBurst(wg, mutex, sock, "unix", TransportRFC5425, b.N+100)
b.ResetTimer()
serverBurst(wg, mutex, sock, "unix", TransportRFC5425, b.N)
wg.Wait()
mutex.Unlock()
return
}