aboutsummaryrefslogtreecommitdiffstats
path: root/core/nonblocking-application_test.go
diff options
context:
space:
mode:
authorJimmy Hu <jimmy.hu@dexon.org>2018-08-15 17:30:46 +0800
committermissionliao <38416648+missionliao@users.noreply.github.com>2018-08-15 17:30:46 +0800
commit39f1d8ae529805fa410d3ed08358c568343705a5 (patch)
treee4f1e44495f5d48df38b7507a01a8f32372eafde /core/nonblocking-application_test.go
parent3a9b545b0f33435c277fcede2251e4b5ae800d40 (diff)
downloaddexon-consensus-39f1d8ae529805fa410d3ed08358c568343705a5.tar
dexon-consensus-39f1d8ae529805fa410d3ed08358c568343705a5.tar.gz
dexon-consensus-39f1d8ae529805fa410d3ed08358c568343705a5.tar.bz2
dexon-consensus-39f1d8ae529805fa410d3ed08358c568343705a5.tar.lz
dexon-consensus-39f1d8ae529805fa410d3ed08358c568343705a5.tar.xz
dexon-consensus-39f1d8ae529805fa410d3ed08358c568343705a5.tar.zst
dexon-consensus-39f1d8ae529805fa410d3ed08358c568343705a5.zip
core: Add a nonBlockingApplication. (#62)
Diffstat (limited to 'core/nonblocking-application_test.go')
-rw-r--r--core/nonblocking-application_test.go97
1 files changed, 97 insertions, 0 deletions
diff --git a/core/nonblocking-application_test.go b/core/nonblocking-application_test.go
new file mode 100644
index 0000000..070eae3
--- /dev/null
+++ b/core/nonblocking-application_test.go
@@ -0,0 +1,97 @@
+// Copyright 2018 The dexon-consensus-core Authors
+// This file is part of the dexon-consensus-core library.
+//
+// The dexon-consensus-core 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-core 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-core 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-core/common"
+)
+
+type slowApp struct {
+ sleep time.Duration
+ stronglyAcked map[common.Hash]struct{}
+ totalOrderingDeliver map[common.Hash]struct{}
+ deliverBlock map[common.Hash]struct{}
+}
+
+func newSlowApp(sleep time.Duration) *slowApp {
+ return &slowApp{
+ sleep: sleep,
+ stronglyAcked: make(map[common.Hash]struct{}),
+ totalOrderingDeliver: make(map[common.Hash]struct{}),
+ deliverBlock: make(map[common.Hash]struct{}),
+ }
+}
+
+func (app *slowApp) StronglyAcked(blockHash common.Hash) {
+ time.Sleep(app.sleep)
+ app.stronglyAcked[blockHash] = struct{}{}
+}
+
+func (app *slowApp) TotalOrderingDeliver(blockHashes common.Hashes, early bool) {
+ time.Sleep(app.sleep)
+ for _, hash := range blockHashes {
+ app.totalOrderingDeliver[hash] = struct{}{}
+ }
+}
+
+func (app *slowApp) DeliverBlock(blockHash common.Hash, timestamp time.Time) {
+ time.Sleep(app.sleep)
+ app.deliverBlock[blockHash] = struct{}{}
+}
+
+type NonBlockingAppTestSuite struct {
+ suite.Suite
+}
+
+func (s *NonBlockingAppTestSuite) TestNonBlockingApplication() {
+ sleep := 50 * time.Millisecond
+ app := newSlowApp(sleep)
+ nbapp := newNonBlockingApplication(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 {
+ nbapp.StronglyAcked(hash)
+ nbapp.DeliverBlock(hash, time.Now().UTC())
+ }
+ nbapp.TotalOrderingDeliver(hashes, true)
+
+ // nonBlockingApplication should be non-blocking.
+ s.True(shouldFinish.After(time.Now().UTC()))
+
+ nbapp.wait()
+ for _, hash := range hashes {
+ s.Contains(app.stronglyAcked, hash)
+ s.Contains(app.totalOrderingDeliver, hash)
+ s.Contains(app.deliverBlock, hash)
+ }
+}
+
+func TestNonBlockingApplication(t *testing.T) {
+ suite.Run(t, new(NonBlockingAppTestSuite))
+}