aboutsummaryrefslogtreecommitdiffstats
path: root/core/nonblocking_test.go
blob: 4816186aa5dd1e9f9bc0bfbec95a8daa92cef717 (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
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
159
160
// Copyright 2018 The dexon-consensus Authors
// This file is part of the dexon-consensus library.
//
// The dexon-consensus library is free software: you can redistribute it
// and/or modify it under the terms of the GNU Lesser General Public License as
// published by the Free Software Foundation, either version 3 of the License,
// or (at your option) any later version.
//
// The dexon-consensus library is distributed in the hope that it will be
// useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser
// General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the dexon-consensus library. If not, see
// <http://www.gnu.org/licenses/>.

package core

import (
    "testing"
    "time"

    "github.com/stretchr/testify/suite"

    "github.com/dexon-foundation/dexon-consensus/common"
    "github.com/dexon-foundation/dexon-consensus/core/types"
)

// slowApp is an Application instance slow things down in every method.
type slowApp struct {
    sleep          time.Duration
    blockConfirmed map[common.Hash]struct{}
    blockDelivered map[common.Hash]struct{}
}

func newSlowApp(sleep time.Duration) *slowApp {
    return &slowApp{
        sleep:          sleep,
        blockConfirmed: make(map[common.Hash]struct{}),
        blockDelivered: make(map[common.Hash]struct{}),
    }
}

func (app *slowApp) PreparePayload(_ types.Position) ([]byte, error) {
    return []byte{}, nil
}

func (app *slowApp) PrepareWitness(_ uint64) (types.Witness, error) {
    return types.Witness{}, nil
}

func (app *slowApp) VerifyBlock(_ *types.Block) types.BlockVerifyStatus {
    return types.VerifyOK
}

func (app *slowApp) BlockConfirmed(block types.Block) {
    time.Sleep(app.sleep)
    app.blockConfirmed[block.Hash] = struct{}{}
}

func (app *slowApp) BlockDelivered(blockHash common.Hash,
    blockPosition types.Position, _ []byte) {
    time.Sleep(app.sleep)
    app.blockDelivered[blockHash] = struct{}{}
}

func (app *slowApp) BlockReceived(hash common.Hash) {}

func (app *slowApp) BlockReady(hash common.Hash) {}

// noDebugApp is to make sure nonBlocking works when Debug interface
// is not implemented by the provided Application instance.
type noDebugApp struct {
    blockConfirmed map[common.Hash]struct{}
    blockDelivered map[common.Hash]struct{}
}

func newNoDebugApp() *noDebugApp {
    return &noDebugApp{
        blockConfirmed: make(map[common.Hash]struct{}),
        blockDelivered: make(map[common.Hash]struct{}),
    }
}

func (app *noDebugApp) PreparePayload(_ types.Position) ([]byte, error) {
    panic("test")
}

func (app *noDebugApp) PrepareWitness(_ uint64) (types.Witness, error) {
    panic("test")
}

func (app *noDebugApp) VerifyBlock(_ *types.Block) types.BlockVerifyStatus {
    panic("test")
}

func (app *noDebugApp) BlockConfirmed(block types.Block) {
    app.blockConfirmed[block.Hash] = struct{}{}
}

func (app *noDebugApp) BlockDelivered(blockHash common.Hash,
    blockPosition types.Position, _ []byte) {
    app.blockDelivered[blockHash] = struct{}{}
}

type NonBlockingTestSuite struct {
    suite.Suite
}

func (s *NonBlockingTestSuite) TestNonBlocking() {
    sleep := 50 * time.Millisecond
    app := newSlowApp(sleep)
    nbModule := newNonBlocking(app, app)
    hashes := make(common.Hashes, 10)
    for idx := range hashes {
        hashes[idx] = common.NewRandomHash()
    }
    now := time.Now().UTC()
    shouldFinish := now.Add(100 * time.Millisecond)

    // Start doing some 'heavy' job.
    for _, hash := range hashes {
        nbModule.BlockConfirmed(types.Block{
            Hash:    hash,
            Witness: types.Witness{},
        })
        nbModule.BlockDelivered(hash, types.Position{}, []byte(nil))
    }

    // nonBlocking should be non-blocking.
    s.True(shouldFinish.After(time.Now().UTC()))

    nbModule.wait()
    for _, hash := range hashes {
        s.Contains(app.blockConfirmed, hash)
        s.Contains(app.blockDelivered, hash)
    }
}

func (s *NonBlockingTestSuite) TestNoDebug() {
    app := newNoDebugApp()
    nbModule := newNonBlocking(app, nil)
    hash := common.NewRandomHash()
    // Test BlockConfirmed.
    nbModule.BlockConfirmed(types.Block{Hash: hash})
    // Test BlockDelivered
    nbModule.BlockDelivered(hash, types.Position{}, []byte(nil))
    nbModule.wait()
    s.Contains(app.blockConfirmed, hash)
    s.Contains(app.blockDelivered, hash)
    // Test other synchronous methods.
    s.Panics(func() { nbModule.PreparePayload(types.Position{}) })
    s.Panics(func() { nbModule.PrepareWitness(0) })
    s.Panics(func() { nbModule.VerifyBlock(nil) })
}

func TestNonBlocking(t *testing.T) {
    suite.Run(t, new(NonBlockingTestSuite))
}