summaryrefslogtreecommitdiff
path: root/srv/src/pow/store_test.go
blob: 324a40c1439aee1afce1be672bdac97476358906 (plain)
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
package pow

import (
	"testing"
	"time"

	"github.com/stretchr/testify/assert"
	"github.com/tilinna/clock"
)

func TestStore(t *testing.T) {
	clock := clock.NewMock(time.Now().Truncate(time.Hour))
	now := clock.Now()

	s := NewMemoryStore(clock)
	defer s.Close()

	seed := []byte{0}

	// mark solved should work
	err := s.MarkSolved(seed, now.Add(time.Second))
	assert.NoError(t, err)

	// mark again, should not work
	err = s.MarkSolved(seed, now.Add(time.Hour))
	assert.ErrorIs(t, err, ErrSeedSolved)

	// marking a different seed should still work
	seed2 := []byte{1}
	err = s.MarkSolved(seed2, now.Add(inMemStoreGCPeriod*2))
	assert.NoError(t, err)
	err = s.MarkSolved(seed2, now.Add(time.Hour))
	assert.ErrorIs(t, err, ErrSeedSolved)

	now = clock.Add(inMemStoreGCPeriod)
	<-s.(*inMemStore).spinLoopCh

	// first one should be markable again, second shouldnt
	err = s.MarkSolved(seed, now.Add(time.Second))
	assert.NoError(t, err)
	err = s.MarkSolved(seed2, now.Add(time.Hour))
	assert.ErrorIs(t, err, ErrSeedSolved)

	now = clock.Add(inMemStoreGCPeriod)
	<-s.(*inMemStore).spinLoopCh

	// now both should be expired
	err = s.MarkSolved(seed, now.Add(time.Second))
	assert.NoError(t, err)
	err = s.MarkSolved(seed2, now.Add(time.Second))
	assert.NoError(t, err)
}