File: id_pool_test.go

package info (click to toggle)
golang-github-ngaut-pools 0.0~git20141008.0.6352e00-5
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 132 kB
  • sloc: makefile: 2
file content (118 lines) | stat: -rw-r--r-- 2,379 bytes parent folder | download | duplicates (3)
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
// Copyright 2014, Google Inc. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package pools

import (
	"reflect"
	"strings"
	"testing"
)

func (pool *IDPool) want(want *IDPool, t *testing.T) {
	if pool.maxUsed != want.maxUsed {
		t.Errorf("pool.maxUsed = %#v, want %#v", pool.maxUsed, want.maxUsed)
	}

	if !reflect.DeepEqual(pool.used, want.used) {
		t.Errorf("pool.used = %#v, want %#v", pool.used, want.used)
	}
}

func TestIDPoolFirstGet(t *testing.T) {
	pool := NewIDPool()

	if got := pool.Get(); got != 1 {
		t.Errorf("pool.Get() = %v, want 1", got)
	}

	pool.want(&IDPool{used: map[uint32]bool{}, maxUsed: 1}, t)
}

func TestIDPoolSecondGet(t *testing.T) {
	pool := NewIDPool()
	pool.Get()

	if got := pool.Get(); got != 2 {
		t.Errorf("pool.Get() = %v, want 2", got)
	}

	pool.want(&IDPool{used: map[uint32]bool{}, maxUsed: 2}, t)
}

func TestIDPoolPutToUsedSet(t *testing.T) {
	pool := NewIDPool()
	id1 := pool.Get()
	pool.Get()
	pool.Put(id1)

	pool.want(&IDPool{used: map[uint32]bool{1: true}, maxUsed: 2}, t)
}

func TestIDPoolPutMaxUsed1(t *testing.T) {
	pool := NewIDPool()
	id1 := pool.Get()
	pool.Put(id1)

	pool.want(&IDPool{used: map[uint32]bool{}, maxUsed: 0}, t)
}

func TestIDPoolPutMaxUsed2(t *testing.T) {
	pool := NewIDPool()
	pool.Get()
	id2 := pool.Get()
	pool.Put(id2)

	pool.want(&IDPool{used: map[uint32]bool{}, maxUsed: 1}, t)
}

func TestIDPoolGetFromUsedSet(t *testing.T) {
	pool := NewIDPool()
	id1 := pool.Get()
	pool.Get()
	pool.Put(id1)

	if got := pool.Get(); got != 1 {
		t.Errorf("pool.Get() = %v, want 1", got)
	}

	pool.want(&IDPool{used: map[uint32]bool{}, maxUsed: 2}, t)
}

func wantError(want string, t *testing.T) {
	rec := recover()
	if rec == nil {
		t.Errorf("expected panic, but there wasn't one")
	}
	err, ok := rec.(error)
	if !ok || !strings.Contains(err.Error(), want) {
		t.Errorf("wrong error, got '%v', want '%v'", err, want)
	}
}

func TestIDPoolPut0(t *testing.T) {
	pool := NewIDPool()
	pool.Get()

	defer wantError("invalid value", t)
	pool.Put(0)
}

func TestIDPoolPutInvalid(t *testing.T) {
	pool := NewIDPool()
	pool.Get()

	defer wantError("invalid value", t)
	pool.Put(5)
}

func TestIDPoolPutDuplicate(t *testing.T) {
	pool := NewIDPool()
	pool.Get()
	pool.Get()
	pool.Put(1)

	defer wantError("already recycled", t)
	pool.Put(1)
}