From 0b23499afb71fb2ce7847b8b5810d2458a7ff589 Mon Sep 17 00:00:00 2001 From: Vlad Date: Tue, 16 Apr 2019 14:39:45 +0200 Subject: [PATCH] swarm/pss: minor refactoring --- swarm/pss/prox_test.go | 38 ++++++++++++++++++-------------------- 1 file changed, 18 insertions(+), 20 deletions(-) diff --git a/swarm/pss/prox_test.go b/swarm/pss/prox_test.go index b007fc18e0..908a0d3302 100644 --- a/swarm/pss/prox_test.go +++ b/swarm/pss/prox_test.go @@ -63,15 +63,15 @@ func (td *testData) pushNotification(val handlerNotification) { td.mu.Unlock() } -func (td *testData) popNotification() (first handlerNotification, ok bool) { +func (td *testData) popNotification() (first handlerNotification, exist bool) { td.mu.Lock() if len(td.notifications) > 0 { - ok = true + exist = true first = td.notifications[0] td.notifications = td.notifications[1:] } td.mu.Unlock() - return first, ok + return first, exist } func (td *testData) getMsgCount() int { @@ -272,33 +272,34 @@ func (td *testData) sendAllMsgs() error { return nil } +func isMoreTimeLeft(ctx context.Context) bool { + select { + case <-ctx.Done(): + return false + default: + return true + } +} + // testRoutine is the main test function, called by Simulation.Run() func testRoutine(td *testData, ctx context.Context) error { + hasMoreRound := func(err error, hadMessage bool) bool { + return err == nil && (hadMessage || isMoreTimeLeft(ctx)) + } + if err := td.sendAllMsgs(); err != nil { return err } - isMoreTimeLeft := func() bool { - select { - case <-ctx.Done(): - return false - default: - return true - } - } - - hasMoreRound := func(err error, hadMessage bool) bool { - return err == nil && (hadMessage || isMoreTimeLeft()) - } - var err error received := 0 hadMessage := false + for oneMoreRound := true; oneMoreRound; oneMoreRound = hasMoreRound(err, hadMessage) { message, hadMessage := td.popNotification() - if !isMoreTimeLeft() { + if !isMoreTimeLeft(ctx) { // Stop handlers from sending more messages. // Note: only best effort, race is possible. td.setDone() @@ -314,8 +315,6 @@ func testRoutine(td *testData, ctx context.Context) error { } else { time.Sleep(32 * time.Millisecond) } - - oneMoreRound = err == nil && (hadMessage || isMoreTimeLeft()) } if err != nil { @@ -326,7 +325,6 @@ func testRoutine(td *testData, ctx context.Context) error { return ctx.Err() } return nil - } func (td *testData) isAllowedMessage(n handlerNotification) bool {