aboutsummaryrefslogtreecommitdiffstats
path: root/swarm/chunk/tag_test.go
blob: e6acfb185bd47398a899d00d103c1ab5e7df6cd8 (plain) (blame)
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
// Copyright 2019 The go-ethereum Authors
// This file is part of the go-ethereum library.
//
// The go-ethereum 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 go-ethereum 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 go-ethereum library. If not, see <http://www.gnu.org/licenses/>.

package chunk

import (
    "bytes"
    "sync"
    "testing"
    "time"
)

var (
    allStates = []State{StateSplit, StateStored, StateSeen, StateSent, StateSynced}
)

// TestTagSingleIncrements tests if Inc increments the tag state value
func TestTagSingleIncrements(t *testing.T) {
    tg := &Tag{total: 10}

    tc := []struct {
        state    uint32
        inc      int
        expcount int64
        exptotal int64
    }{
        {state: StateSplit, inc: 10, expcount: 10, exptotal: 10},
        {state: StateStored, inc: 9, expcount: 9, exptotal: 9},
        {state: StateSeen, inc: 1, expcount: 1, exptotal: 10},
        {state: StateSent, inc: 9, expcount: 9, exptotal: 9},
        {state: StateSynced, inc: 9, expcount: 9, exptotal: 9},
    }

    for _, tc := range tc {
        for i := 0; i < tc.inc; i++ {
            tg.Inc(tc.state)
        }
    }

    for _, tc := range tc {
        if tg.Get(tc.state) != tc.expcount {
            t.Fatalf("not incremented")
        }
    }
}

// TestTagStatus is a unit test to cover Tag.Status method functionality
func TestTagStatus(t *testing.T) {
    tg := &Tag{total: 10}
    tg.Inc(StateSeen)
    tg.Inc(StateSent)
    tg.Inc(StateSynced)

    for i := 0; i < 10; i++ {
        tg.Inc(StateSplit)
        tg.Inc(StateStored)
    }
    for _, v := range []struct {
        state    State
        expVal   int64
        expTotal int64
    }{
        {state: StateStored, expVal: 10, expTotal: 10},
        {state: StateSplit, expVal: 10, expTotal: 10},
        {state: StateSeen, expVal: 1, expTotal: 10},
        {state: StateSent, expVal: 1, expTotal: 9},
        {state: StateSynced, expVal: 1, expTotal: 9},
    } {
        val, total, err := tg.Status(v.state)
        if err != nil {
            t.Fatal(err)
        }
        if val != v.expVal {
            t.Fatalf("should be %d, got %d", v.expVal, val)
        }
        if total != v.expTotal {
            t.Fatalf("expected total to be %d, got %d", v.expTotal, total)
        }
    }
}

// tests ETA is precise
func TestTagETA(t *testing.T) {
    now := time.Now()
    maxDiff := 100000 // 100 microsecond
    tg := &Tag{total: 10, startedAt: now}
    time.Sleep(100 * time.Millisecond)
    tg.Inc(StateSplit)
    eta, err := tg.ETA(StateSplit)
    if err != nil {
        t.Fatal(err)
    }
    diff := time.Until(eta) - 9*time.Since(now)
    if int(diff) > maxDiff {
        t.Fatalf("ETA is not precise, got diff %v > .1ms", diff)
    }
}

// TestTagConcurrentIncrements tests Inc calls concurrently
func TestTagConcurrentIncrements(t *testing.T) {
    tg := &Tag{}
    n := 1000
    wg := sync.WaitGroup{}
    wg.Add(5 * n)
    for _, f := range allStates {
        go func(f State) {
            for j := 0; j < n; j++ {
                go func() {
                    tg.Inc(f)
                    wg.Done()
                }()
            }
        }(f)
    }
    wg.Wait()
    for _, f := range allStates {
        v := tg.Get(f)
        if v != int64(n) {
            t.Fatalf("expected state %v to be %v, got %v", f, n, v)
        }
    }
}

// TestTagsMultipleConcurrentIncrements tests Inc calls concurrently
func TestTagsMultipleConcurrentIncrementsSyncMap(t *testing.T) {
    ts := NewTags()
    n := 100
    wg := sync.WaitGroup{}
    wg.Add(10 * 5 * n)
    for i := 0; i < 10; i++ {
        s := string([]byte{uint8(i)})
        tag, err := ts.New(s, int64(n))
        if err != nil {
            t.Fatal(err)
        }
        for _, f := range allStates {
            go func(tag *Tag, f State) {
                for j := 0; j < n; j++ {
                    go func() {
                        tag.Inc(f)
                        wg.Done()
                    }()
                }
            }(tag, f)
        }
    }
    wg.Wait()
    i := 0
    ts.Range(func(k, v interface{}) bool {
        i++
        uid := k.(uint32)
        for _, f := range allStates {
            tag, err := ts.Get(uid)
            if err != nil {
                t.Fatal(err)
            }
            stateVal := tag.Get(f)
            if stateVal != int64(n) {
                t.Fatalf("expected tag %v state %v to be %v, got %v", uid, f, n, v)
            }
        }
        return true

    })
    if i != 10 {
        t.Fatal("not enough tagz")
    }
}

// TestMarshallingWithAddr tests that marshalling and unmarshalling is done correctly when the
// tag Address (byte slice) contains some arbitrary value
func TestMarshallingWithAddr(t *testing.T) {
    tg := NewTag(111, "test/tag", 10)
    tg.Address = []byte{0, 1, 2, 3, 4, 5, 6}

    for _, f := range allStates {
        tg.Inc(f)
    }

    b, err := tg.MarshalBinary()
    if err != nil {
        t.Fatal(err)
    }

    unmarshalledTag := &Tag{}
    err = unmarshalledTag.UnmarshalBinary(b)
    if err != nil {
        t.Fatal(err)
    }

    if unmarshalledTag.Uid != tg.Uid {
        t.Fatalf("tag uids not equal. want %d got %d", tg.Uid, unmarshalledTag.Uid)
    }

    if unmarshalledTag.Name != tg.Name {
        t.Fatalf("tag names not equal. want %s got %s", tg.Name, unmarshalledTag.Name)
    }

    for _, state := range allStates {
        uv, tv := unmarshalledTag.Get(state), tg.Get(state)
        if uv != tv {
            t.Fatalf("state %d inconsistent. expected %d to equal %d", state, uv, tv)
        }
    }

    if unmarshalledTag.Total() != tg.Total() {
        t.Fatalf("tag names not equal. want %d got %d", tg.Total(), unmarshalledTag.Total())
    }

    if len(unmarshalledTag.Address) != len(tg.Address) {
        t.Fatalf("tag addresses length mismatch, want %d, got %d", len(tg.Address), len(unmarshalledTag.Address))
    }

    if !bytes.Equal(unmarshalledTag.Address, tg.Address) {
        t.Fatalf("expected tag address to be %v got %v", unmarshalledTag.Address, tg.Address)
    }
}

// TestMarshallingNoAddress tests that marshalling and unmarshalling is done correctly
// when the tag Address (byte slice) is empty in this case
func TestMarshallingNoAddr(t *testing.T) {
    tg := NewTag(111, "test/tag", 10)
    for _, f := range allStates {
        tg.Inc(f)
    }

    b, err := tg.MarshalBinary()
    if err != nil {
        t.Fatal(err)
    }

    unmarshalledTag := &Tag{}
    err = unmarshalledTag.UnmarshalBinary(b)
    if err != nil {
        t.Fatal(err)
    }

    if unmarshalledTag.Uid != tg.Uid {
        t.Fatalf("tag uids not equal. want %d got %d", tg.Uid, unmarshalledTag.Uid)
    }

    if unmarshalledTag.Name != tg.Name {
        t.Fatalf("tag names not equal. want %s got %s", tg.Name, unmarshalledTag.Name)
    }

    for _, state := range allStates {
        uv, tv := unmarshalledTag.Get(state), tg.Get(state)
        if uv != tv {
            t.Fatalf("state %d inconsistent. expected %d to equal %d", state, uv, tv)
        }
    }

    if unmarshalledTag.Total() != tg.Total() {
        t.Fatalf("tag names not equal. want %d got %d", tg.Total(), unmarshalledTag.Total())
    }

    if len(unmarshalledTag.Address) != len(tg.Address) {
        t.Fatalf("expected tag addresses to be equal length")
    }
}