Skip to content

Commit

Permalink
test: unit test for request cache of stream server
Browse files Browse the repository at this point in the history
Signed-off-by: YaoZengzeng <[email protected]>
  • Loading branch information
YaoZengzeng committed Jul 23, 2018
1 parent 2f0b5cf commit 025e619
Showing 1 changed file with 75 additions and 0 deletions.
75 changes: 75 additions & 0 deletions cri/stream/request_cache_test.go
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")
}
}

0 comments on commit 025e619

Please sign in to comment.