aboutsummaryrefslogtreecommitdiffstats
path: root/ethreact/reactor_test.go
blob: 801a8abd01f56344b8fa14fed0d10675085b167c (plain) (blame)
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
package ethreact

import (
    "fmt"
    "testing"
)

func TestReactorAdd(t *testing.T) {
    reactor := New()
    ch := make(chan Event)
    reactor.Subscribe("test", ch)
    if reactor.eventHandlers["test"] == nil {
        t.Error("Expected new eventHandler to be created")
    }
    reactor.Unsubscribe("test", ch)
    if reactor.eventHandlers["test"] != nil {
        t.Error("Expected eventHandler to be removed")
    }
}

func TestReactorEvent(t *testing.T) {
    var name string
    reactor := New()
    // Buffer the channel, so it doesn't block for this test
    cap := 20
    ch := make(chan Event, cap)
    reactor.Subscribe("even", ch)
    reactor.Subscribe("odd", ch)
    reactor.Post("even", "disappears") // should not broadcast if engine not started
    reactor.Start()
    for i := 0; i < cap; i++ {
        if i%2 == 0 {
            name = "even"
        } else {
            name = "odd"
        }
        reactor.Post(name, i)
    }
    reactor.Post("test", cap) // this should not block
    i := 0
    reactor.Flush()
    close(ch)
    for event := range ch {
        fmt.Printf("%d: %v", i, event)
        if i%2 == 0 {
            name = "even"
        } else {
            name = "odd"
        }
        if val, ok := event.Resource.(int); ok {
            if i != val || event.Name != name {
                t.Error("Expected event %d to be of type %s and resource %d, got ", i, name, i, val)
            }
        } else {
            t.Error("Unable to cast")
        }
        i++
    }
    if i != cap {
        t.Error("excpected exactly %d events, got ", i)
    }
    reactor.Stop()
}