Skip to content

Commit 005174f

Browse files
committed
Added unit test for bandwidth
1 parent 9ad1317 commit 005174f

File tree

3 files changed

+48
-1
lines changed

3 files changed

+48
-1
lines changed

src/github.com/getlantern/bandwidth/bandwidth.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ var (
1717
mutex sync.RWMutex
1818

1919
// Updates is a channel on which one can receive updates to the Quota
20-
Updates = make(chan *Quota)
20+
Updates = make(chan *Quota, 100)
2121
)
2222

2323
// Quota encapsulates information about the user's bandwidth quota.
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package bandwidth
2+
3+
import (
4+
"fmt"
5+
"net/http"
6+
"testing"
7+
"time"
8+
9+
"github.com/getlantern/eventual"
10+
"github.com/stretchr/testify/assert"
11+
)
12+
13+
func TestRoundTrip(t *testing.T) {
14+
fromChannel := eventual.NewValue()
15+
go func() {
16+
for q := range Updates {
17+
fromChannel.Set(q)
18+
}
19+
}()
20+
21+
Track(build(5, 500, 2))
22+
Track(build(6, 500, 3))
23+
Track(build(4, 500, 1))
24+
25+
q := GetQuota()
26+
time.Sleep(50 * time.Millisecond)
27+
_q2, ok := fromChannel.Get(1 * time.Second)
28+
if assert.True(t, ok, "Should have gotten quota from channel") {
29+
q2 := _q2.(*Quota)
30+
assert.EqualValues(t, 500, q.MiBAllowed)
31+
assert.EqualValues(t, 500, q2.MiBAllowed)
32+
assert.EqualValues(t, 6, q.MiBUsed)
33+
assert.EqualValues(t, 6, q2.MiBUsed)
34+
assert.EqualValues(t, time.Unix(0, 3), q.AsOf)
35+
assert.EqualValues(t, time.Unix(0, 3), q2.AsOf)
36+
}
37+
38+
}
39+
40+
func build(used uint64, allowed uint64, asof int64) *http.Response {
41+
resp := &http.Response{
42+
Header: make(http.Header),
43+
}
44+
resp.Header.Set("XBQ", fmt.Sprintf("%d/%d/%d", used, allowed, asof))
45+
return resp
46+
}

testpackages.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
github.com/getlantern/appdir
22
github.com/getlantern/balancer
3+
github.com/getlantern/bandwidth
34
github.com/getlantern/buuid
45
github.com/getlantern/bytecounting
56
github.com/getlantern/byteexec

0 commit comments

Comments
 (0)