-
Notifications
You must be signed in to change notification settings - Fork 0
/
events_test.go
99 lines (92 loc) · 1.76 KB
/
events_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
package backive
import (
"encoding/json"
"fmt"
"io"
"log"
"net"
"os"
"testing"
"time"
)
type ConnStub struct {
}
var counter int
func (c ConnStub) Read(b []byte) (int, error) {
switch {
case counter == 0:
counter++
env := map[string]string{}
env["test"] = "test"
message := map[string]interface{}{}
message["request"] = "udev"
message["data"] = env
data, err := json.Marshal(message)
copy(b, data)
log.Println(string(b))
return len(data), err
case counter == 1:
counter++
return 0, io.EOF
case counter == 2:
counter++
return 0, fmt.Errorf("Some Error for testing")
default:
return 0, io.EOF
}
}
func (c ConnStub) Close() error {
return nil
}
func (c ConnStub) LocalAddr() net.Addr {
return nil
}
func (c ConnStub) RemoteAddr() net.Addr {
return nil
}
func (c ConnStub) SetDeadline(t time.Time) error {
return nil
}
func (c ConnStub) SetReadDeadline(t time.Time) error {
return nil
}
func (c ConnStub) SetWriteDeadline(t time.Time) error {
return nil
}
func (c ConnStub) Write(b []byte) (int, error) {
return 0, nil
}
var hasBeenCalled = false
func TestEventhandler(t *testing.T) {
eh := new(EventHandler)
err := eh.Init("./backive.socket")
if err != nil {
t.Fail()
}
defer func() {
err := os.Remove("./backive.socket")
if err != nil {
t.Log(err)
}
}()
t.Log("Initialized test")
//var hasBeenCalled = make(chan bool)
eh.RegisterCallback(
func(m map[string]string) {
t.Log("Callback got called")
hasBeenCalled = true
},
)
t.Log("registered callback")
mockAccept = func(eh *EventHandler) (net.Conn, error) {
t.Log("Mocked Accept() has been called.")
mycon := ConnStub{}
return mycon, nil
}
eh.process()
//beenCalled := <-hasBeenCalled
if !hasBeenCalled {
t.Log("Got false, need true.")
t.Fail()
}
}