-
Notifications
You must be signed in to change notification settings - Fork 12
/
cleaner_test.go
53 lines (43 loc) · 1.77 KB
/
cleaner_test.go
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
// Copyright 2019 the orbs-network-go authors
// This file is part of the orbs-network-go library in the Orbs project.
//
// This source code is licensed under the MIT license found in the LICENSE file in the root directory of this source tree.
// The above notice should be included in all copies or substantial portions of the software.
package transactionpool
import (
"context"
"github.com/orbs-network/orbs-network-go/test/with"
"github.com/orbs-network/orbs-spec/types/go/primitives"
"github.com/stretchr/testify/require"
"testing"
"time"
)
var tickInterval = func() time.Duration { return 1 * time.Millisecond }
var expiration = func() time.Duration { return 30 * time.Minute }
func TestTicksOnSchedule(t *testing.T) {
with.Concurrency(t, func(ctx context.Context, harness *with.ConcurrencyHarness) {
ts := primitives.TimestampNano(time.Now().UnixNano())
m := aCleaner()
harness.Supervise(startCleaningProcess(ctx, "", tickInterval, expiration, m, func() (primitives.BlockHeight, primitives.TimestampNano, primitives.TimestampSeconds) {
return 0, ts, 0
}, harness.Logger))
// waiting multiple times to assert that ticker is looping :)
for i := 0; i < 3; i++ {
select {
case cleaned := <-m.cleaned:
require.InDelta(t, int64(ts-primitives.TimestampNano(expiration())), int64(cleaned), float64(1*time.Second), "did not call cleaner with expected time")
case <-time.After(tickInterval() * 100):
t.Fatalf("did not call cleaner within expected timeframe")
}
}
})
}
type mockCleaner struct {
cleaned chan primitives.TimestampNano
}
func (c *mockCleaner) clearTransactionsOlderThan(ctx context.Context, ts primitives.TimestampNano) {
c.cleaned <- ts
}
func aCleaner() *mockCleaner {
return &mockCleaner{cleaned: make(chan primitives.TimestampNano, 1)}
}