-
Notifications
You must be signed in to change notification settings - Fork 949
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: unit test for request cache of stream server
Signed-off-by: YaoZengzeng <[email protected]>
- Loading branch information
1 parent
2f0b5cf
commit 025e619
Showing
1 changed file
with
75 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
package stream | ||
|
||
import ( | ||
"testing" | ||
) | ||
|
||
// TODO: use fake clock to test gc of request cache. | ||
|
||
func TestRequestCacheBasic(t *testing.T) { | ||
var tokens []string | ||
r := NewRequestCache() | ||
|
||
n := 10 | ||
for i := 0; i < n; i++ { | ||
token, err := r.Insert(i) | ||
if err != nil { | ||
t.Fatalf("unexpected error when inserting the request: %v", err) | ||
} | ||
tokens = append(tokens, token) | ||
} | ||
|
||
for i := 0; i < n; i++ { | ||
req, found := r.Consume(tokens[i]) | ||
if !found { | ||
t.Fatalf("unexpected error when comsuming the cached request") | ||
} | ||
r, ok := req.(int) | ||
if !ok { | ||
t.Fatalf("the type of cached request has been changed") | ||
} | ||
if r != i { | ||
t.Fatalf("the value of cached request has been changed") | ||
} | ||
} | ||
} | ||
|
||
func TestRequestCacheNonExist(t *testing.T) { | ||
r := NewRequestCache() | ||
token := "non-exist" | ||
_, found := r.Consume(token) | ||
if found { | ||
t.Fatalf("should not find the request that not exist") | ||
} | ||
} | ||
|
||
func TestRequestCacheTokenUnique(t *testing.T) { | ||
r := NewRequestCache() | ||
tokens := make(map[string]bool) | ||
for i := 0; i < MaxInFlight; i++ { | ||
token, err := r.Insert(i) | ||
if err != nil { | ||
t.Fatalf("unexpected error when inserting the request: %v", err) | ||
} | ||
if tokens[token] { | ||
t.Fatalf("the returned token should be unique") | ||
} | ||
tokens[token] = true | ||
} | ||
} | ||
|
||
func TestRequestCacheMaxInFlight(t *testing.T) { | ||
r := NewRequestCache() | ||
|
||
var i int | ||
for i = 0; i < MaxInFlight; i++ { | ||
_, err := r.Insert(i) | ||
if err != nil { | ||
t.Fatalf("unexpected error when inserting the request: %v", err) | ||
} | ||
} | ||
_, err := r.Insert(i) | ||
if err == nil { | ||
t.Fatalf("should report error when there are too many cached request") | ||
} | ||
} |