-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrackvisited_test.go
66 lines (55 loc) · 1.54 KB
/
trackvisited_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
package main
import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/suite"
"net/url"
"testing"
"time"
)
type TrackvisitedTestSuite struct {
suite.Suite
}
// Make sure that VariableThatShouldStartAtFive is set to five
// before each test
func (suite *TrackvisitedTestSuite) SetupTest() {}
// In order for 'go test' to run this suite, we need to create
// a normal test function and pass our suite to suite.Run
func TestTrackvisitedTestSuite(t *testing.T) {
suite.Run(t, new(TrackvisitedTestSuite))
}
func (suite *TrackvisitedTestSuite) TestUrlToMarkdownUri() {
suite.T().Run("keeps thread safe track of visited urls", func(t *testing.T) {
tracker := trackVisitedUrls()
uri, _ := url.Parse("http://myurl.net/my-test-url")
numer_of_runs := 100
swarm := make([]int, numer_of_runs)
results := make(chan bool)
granted_locks := 0
rejected_locks := 0
// firing 100 requests in
// parallel into the service
// tracking the results in
// the results channel
for range swarm {
go func() {
results <- (visitedUpdateRequest{url: *uri}).dispatch(tracker)
}()
}
terminate := time.After(time.Second / 200)
for true {
select {
case res := <-results:
if res == true {
granted_locks += 1
} else {
rejected_locks += 1
}
continue
case <-terminate:
}
break
}
assert.Equal(t, 1, granted_locks, "the service allowed more than one client to reserver the resource!")
assert.Equal(t, numer_of_runs-1, rejected_locks, "the test didn't finish all the runs before the timeout")
})
}