evictedqueue_test.go 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. // Copyright 2019, OpenCensus Authors
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package trace
  15. import (
  16. "reflect"
  17. "testing"
  18. )
  19. func init() {
  20. }
  21. func TestAdd(t *testing.T) {
  22. q := newEvictedQueue(3)
  23. q.add("value1")
  24. q.add("value2")
  25. if wantLen, gotLen := 2, len(q.queue); wantLen != gotLen {
  26. t.Errorf("got queue length %d want %d", gotLen, wantLen)
  27. }
  28. }
  29. func (eq *evictedQueue) queueToArray() []string {
  30. arr := make([]string, 0)
  31. for _, value := range eq.queue {
  32. arr = append(arr, value.(string))
  33. }
  34. return arr
  35. }
  36. func TestDropCount(t *testing.T) {
  37. q := newEvictedQueue(3)
  38. q.add("value1")
  39. q.add("value2")
  40. q.add("value3")
  41. q.add("value1")
  42. q.add("value4")
  43. if wantLen, gotLen := 3, len(q.queue); wantLen != gotLen {
  44. t.Errorf("got queue length %d want %d", gotLen, wantLen)
  45. }
  46. if wantDropCount, gotDropCount := 2, q.droppedCount; wantDropCount != gotDropCount {
  47. t.Errorf("got drop count %d want %d", gotDropCount, wantDropCount)
  48. }
  49. wantArr := []string{"value3", "value1", "value4"}
  50. gotArr := q.queueToArray()
  51. if wantLen, gotLen := len(wantArr), len(gotArr); gotLen != wantLen {
  52. t.Errorf("got array len %d want %d", gotLen, wantLen)
  53. }
  54. if !reflect.DeepEqual(gotArr, wantArr) {
  55. t.Errorf("got array = %#v; want %#v", gotArr, wantArr)
  56. }
  57. }