-
Notifications
You must be signed in to change notification settings - Fork 1
/
monoton_test.go
182 lines (156 loc) · 3.96 KB
/
monoton_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
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
// Copyright 2021 Mustafa Turan. All rights reserved.
// Use of this source code is governed by a Apache License 2.0 license that can
// be found in the LICENSE file.
package monoton
import (
"bytes"
"errors"
"math"
"strings"
"testing"
"github.com/mustafaturan/monoton/v3/sequencer"
)
func TestNew(t *testing.T) {
tests := []struct {
s sequencer.Sequencer
node uint64
initialTime uint64
wantErr error
wantNode string
wantTime uint64
}{
{
&validSequencer{},
3843,
uint64(1),
nil,
"zz",
uint64(1),
},
}
errorTests := []struct {
s sequencer.Sequencer
node uint64
initialTime uint64
wantErr error
wantNode string
wantTime uint64
}{
{
&validSequencer{},
3844,
uint64(2),
errors.New("node can't be greater than 3843 (given 3844)"),
"",
uint64(2),
},
{
&invalidSequencer{},
1,
uint64(0),
errors.New("max byte size sum of sequence(8) and time sequence(8) can't be >= total byte size(16), at least 1 byte slot is needed for node"),
"",
uint64(0),
},
}
configureMsg := "New(%v, %d, %d) want: %v, got: %v"
nodeMsg := "New(%v, %d, _) want node: %s, got node: %s"
timeMsg := "New(%v, _, %d) want time: %d, got time: %d"
for _, test := range tests {
test := test
got, err := New(test.s, test.node, test.initialTime)
t.Run("assigns node val correctly", func(t *testing.T) {
if string(got.node) != test.wantNode {
t.Errorf(nodeMsg, test.s, test.node, test.wantNode, got.node)
}
})
t.Run("assigns initialTime val correctly", func(t *testing.T) {
if got.initialTime != test.wantTime {
t.Errorf(timeMsg, test.s, test.initialTime, test.wantTime, got.initialTime)
}
})
t.Run("must not have error", func(t *testing.T) {
if err != test.wantErr {
t.Errorf("want %+v but got %+v", test.wantErr, err)
}
})
}
for _, test := range errorTests {
test := test
_, err := New(test.s, test.node, test.initialTime)
t.Run("errors with correct message", func(t *testing.T) {
if err != test.wantErr && err.Error() != test.wantErr.Error() {
t.Errorf(configureMsg, test.s, test.node, test.initialTime, test.wantErr, err.Error())
}
})
}
}
func TestNext(t *testing.T) {
m, _ := New(&validSequencer{}, 3843, 0)
m1, m2 := m.Next(), m.Next()
t.Run("generates greater sequences on each call", func(t *testing.T) {
t.Parallel()
if strings.Compare(m1, m2) >= 0 {
t.Errorf("Next(): %s >= Next(): %s", m1, m2)
}
})
t.Run("generates 16 bytes string sequences", func(t *testing.T) {
t.Parallel()
results := []string{m1, m2}
for _, r := range results {
if len(r) != 16 {
t.Errorf("Next(): %s couldn't produce 16 bytes string", r)
}
}
})
}
func TestNextBytes(t *testing.T) {
m, _ := New(&validSequencer{}, 3843, 0)
m1, m2 := m.NextBytes(), m.NextBytes()
t.Run("generates greater sequences on each call", func(t *testing.T) {
t.Parallel()
if bytes.Compare(m1[:], m2[:]) >= 0 {
t.Errorf("Next(): %s >= Next(): %s", m1, m2)
}
})
t.Run("generates 16 bytes sequences", func(t *testing.T) {
t.Parallel()
results := [][]byte{m1[:], m2[:]}
for _, r := range results {
if len(r) != 16 {
t.Errorf("Next(): %s couldn't produce 16 bytes", r)
}
}
})
}
type validSequencer struct {
counter uint64
}
type invalidSequencer struct {
counter uint64
}
func (v *validSequencer) MaxNode() uint64 {
return uint64(math.Pow(62, 2)) - 1
}
func (v *validSequencer) MaxTime() uint64 {
return uint64(math.Pow(62, 8)) - 1
}
func (v *validSequencer) Max() uint64 {
return uint64(math.Pow(62, 6)) - 1
}
func (v *validSequencer) Next() (uint64, uint64) {
v.counter++
return 1, v.counter
}
func (i *invalidSequencer) MaxNode() uint64 {
return uint64(math.Pow(62, 3)) - 1
}
func (i *invalidSequencer) MaxTime() uint64 {
return uint64(math.Pow(62, 8)) - 1
}
func (i *invalidSequencer) Max() uint64 {
return uint64(math.Pow(62, 8)) - 1
}
func (i *invalidSequencer) Next() (uint64, uint64) {
return 1, i.counter
}