aboutsummaryrefslogtreecommitdiffstats
path: root/dex/nodetable_test.go
blob: 5adf0fbece2b08822bc355c87225bebea8b4dcba (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
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
package dex

import (
    "crypto/ecdsa"
    "net"
    "testing"
    "time"

    "github.com/dexon-foundation/dexon/common"
    "github.com/dexon-foundation/dexon/crypto"
    "github.com/dexon-foundation/dexon/p2p/enode"
)

func TestNodeTable(t *testing.T) {
    table := newNodeTable()
    ch := make(chan newMetasEvent)
    table.SubscribeNewMetasEvent(ch)

    metas1 := []*NodeMeta{
        &NodeMeta{ID: randomID()},
        &NodeMeta{ID: randomID()},
    }

    metas2 := []*NodeMeta{
        &NodeMeta{ID: randomID()},
        &NodeMeta{ID: randomID()},
    }

    go table.Add(metas1)

    select {
    case newMetas := <-ch:
        m := map[common.Hash]struct{}{}
        for _, meta := range newMetas.Metas {
            m[meta.Hash()] = struct{}{}
        }

        if len(m) != len(metas1) {
            t.Errorf("len mismatch: got %d, want: %d",
                len(m), len(metas1))
        }

        for _, meta := range metas1 {
            if _, ok := m[meta.Hash()]; !ok {
                t.Errorf("expected meta (%s) not exists", meta.Hash())
            }
        }
    case <-time.After(1 * time.Second):
        t.Error("did not receive new metas event within one second")
    }

    go table.Add(metas2)
    select {
    case newMetas := <-ch:
        m := map[common.Hash]struct{}{}
        for _, meta := range newMetas.Metas {
            m[meta.Hash()] = struct{}{}
        }

        if len(m) != len(metas1) {
            t.Errorf("len mismatch: got %d, want: %d",
                len(m), len(metas2))
        }

        for _, meta := range metas2 {
            if _, ok := m[meta.Hash()]; !ok {
                t.Errorf("expected meta (%s) not exists", meta.Hash())
            }
        }
    case <-time.After(1 * time.Second):
        t.Error("did not receive new metas event within one second")
    }

    var metas []*NodeMeta
    metas = append(metas, metas1...)
    metas = append(metas, metas2...)
    allMetas := table.Metas()
    if len(allMetas) != len(metas) {
        t.Errorf("all metas num mismatch: got %d, want %d",
            len(metas), len(allMetas))
    }

    for _, m := range metas {
        if m.Hash() != table.Get(m.ID).Hash() {
            t.Errorf("meta (%s) mismatch", m.ID.String())
        }
    }
}

func randomEnode() *enode.Node {
    var err error
    var privkey *ecdsa.PrivateKey
    for {
        privkey, err = crypto.GenerateKey()
        if err == nil {
            break
        }
    }
    return enode.NewV4(&privkey.PublicKey, net.IP{}, 0, 0)
}

func randomID() enode.ID {
    return randomEnode().ID()
}