1
2
3
4
5 package context
6
7 import (
8 "net/http"
9 "testing"
10 )
11
12 type keyType int
13
14 const (
15 key1 keyType = iota
16 key2
17 )
18
19 func TestContext(t *testing.T) {
20 assertEqual := func(val interface{}, exp interface{}) {
21 if val != exp {
22 t.Errorf("Expected %v, got %v.", exp, val)
23 }
24 }
25
26 r, _ := http.NewRequest("GET", "http://localhost:8080/", nil)
27 emptyR, _ := http.NewRequest("GET", "http://localhost:8080/", nil)
28
29
30 assertEqual(Get(r, key1), nil)
31
32
33 Set(r, key1, "1")
34 assertEqual(Get(r, key1), "1")
35 assertEqual(len(data[r]), 1)
36
37 Set(r, key2, "2")
38 assertEqual(Get(r, key2), "2")
39 assertEqual(len(data[r]), 2)
40
41
42 value, ok := GetOk(r, key1)
43 assertEqual(value, "1")
44 assertEqual(ok, true)
45
46 value, ok = GetOk(r, "not exists")
47 assertEqual(value, nil)
48 assertEqual(ok, false)
49
50 Set(r, "nil value", nil)
51 value, ok = GetOk(r, "nil value")
52 assertEqual(value, nil)
53 assertEqual(ok, true)
54
55
56 values := GetAll(r)
57 assertEqual(len(values), 3)
58
59
60 values = GetAll(emptyR)
61 if values != nil {
62 t.Error("GetAll didn't return nil value for invalid request")
63 }
64
65
66 values, ok = GetAllOk(r)
67 assertEqual(len(values), 3)
68 assertEqual(ok, true)
69
70
71 values, ok = GetAllOk(emptyR)
72 assertEqual(len(values), 0)
73 assertEqual(ok, false)
74
75
76 Delete(r, key1)
77 assertEqual(Get(r, key1), nil)
78 assertEqual(len(data[r]), 2)
79
80 Delete(r, key2)
81 assertEqual(Get(r, key2), nil)
82 assertEqual(len(data[r]), 1)
83
84
85 Clear(r)
86 assertEqual(len(data), 0)
87 }
88
89 func parallelReader(r *http.Request, key string, iterations int, wait, done chan struct{}) {
90 <-wait
91 for i := 0; i < iterations; i++ {
92 Get(r, key)
93 }
94 done <- struct{}{}
95
96 }
97
98 func parallelWriter(r *http.Request, key, value string, iterations int, wait, done chan struct{}) {
99 <-wait
100 for i := 0; i < iterations; i++ {
101 Set(r, key, value)
102 }
103 done <- struct{}{}
104
105 }
106
107 func benchmarkMutex(b *testing.B, numReaders, numWriters, iterations int) {
108
109 b.StopTimer()
110 r, _ := http.NewRequest("GET", "http://localhost:8080/", nil)
111 done := make(chan struct{})
112 b.StartTimer()
113
114 for i := 0; i < b.N; i++ {
115 wait := make(chan struct{})
116
117 for i := 0; i < numReaders; i++ {
118 go parallelReader(r, "test", iterations, wait, done)
119 }
120
121 for i := 0; i < numWriters; i++ {
122 go parallelWriter(r, "test", "123", iterations, wait, done)
123 }
124
125 close(wait)
126
127 for i := 0; i < numReaders+numWriters; i++ {
128 <-done
129 }
130
131 }
132
133 }
134
135 func BenchmarkMutexSameReadWrite1(b *testing.B) {
136 benchmarkMutex(b, 1, 1, 32)
137 }
138 func BenchmarkMutexSameReadWrite2(b *testing.B) {
139 benchmarkMutex(b, 2, 2, 32)
140 }
141 func BenchmarkMutexSameReadWrite4(b *testing.B) {
142 benchmarkMutex(b, 4, 4, 32)
143 }
144 func BenchmarkMutex1(b *testing.B) {
145 benchmarkMutex(b, 2, 8, 32)
146 }
147 func BenchmarkMutex2(b *testing.B) {
148 benchmarkMutex(b, 16, 4, 64)
149 }
150 func BenchmarkMutex3(b *testing.B) {
151 benchmarkMutex(b, 1, 2, 128)
152 }
153 func BenchmarkMutex4(b *testing.B) {
154 benchmarkMutex(b, 128, 32, 256)
155 }
156 func BenchmarkMutex5(b *testing.B) {
157 benchmarkMutex(b, 1024, 2048, 64)
158 }
159 func BenchmarkMutex6(b *testing.B) {
160 benchmarkMutex(b, 2048, 1024, 512)
161 }
162
View as plain text