-
Notifications
You must be signed in to change notification settings - Fork 1
/
example_02-server_test.go
101 lines (77 loc) · 1.83 KB
/
example_02-server_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
package syslog5424
import (
"fmt"
"log"
"os"
"sync"
"time"
)
const TestSOCKET string = "./test.socket"
func ExampleListener_basic() {
defer os.Remove(TestSOCKET)
wg := new(sync.WaitGroup)
mutex := new(sync.Mutex)
mutex.Lock()
Now = func() time.Time {
t, _ := time.ParseInLocation("2006-01-02T15:04:00Z", "2014-12-20T14:04:00Z", time.UTC)
return t
}
wg.Add(2)
go server(wg, mutex)
go client(wg, mutex)
wg.Wait()
mutex.Unlock()
// Output:
// <27>1 2014-12-20T14:04:00Z localhost client-app 1234 - - ERR : doing some stuff
// <27>1 2014-12-20T14:04:00Z localhost client-app 1234 - - ERR : doing anoter stuff
// <27>1 2014-12-20T14:04:00Z localhost client-app 1234 - - ERR : doing a last stuff
}
func client(wg *sync.WaitGroup, mutex *sync.Mutex) {
defer wg.Done()
// waiting the creation of the socket
mutex.Lock()
slConn, chanErr, err := Dial("unix", TestSOCKET)
if err != nil {
log.Fatal(err)
}
go func() {
if err := <-chanErr; err != nil {
log.Fatal(err)
}
}()
syslog, err := New(slConn, LogDAEMON|LogWARNING, "client-app")
if err != nil {
log.Fatal(err)
}
syslog.TestMode()
loggerErrConf := syslog.Channel(LogERR).Logger("ERR : ")
loggerErrConf.Print("doing some stuff")
loggerErrConf.Print("doing anoter stuff")
loggerErrConf.Print("doing a last stuff")
slConn.End()
}
func server(wg *sync.WaitGroup, mutex *sync.Mutex) {
defer wg.Done()
listener, err := UnixListener(TestSOCKET)
if err != nil {
log.Fatal(err)
}
collect, chanErr := NewReceiver(listener, 100, TransportZeroEnded)
go func() {
if err := <-chanErr; err != nil {
log.Fatalf("client chanErr %q", err)
}
}()
// socket is created
mutex.Unlock()
count := 3
for count > 0 {
count--
msg, err, _ := collect.Receive()
if err != nil {
log.Fatal(err)
}
fmt.Printf("%s\n", msg.String())
}
collect.End()
}