~nskaggs/+junk/xenial-test

« back to all changes in this revision

Viewing changes to src/github.com/juju/mempool/pool_test.go

  • Committer: Nicholas Skaggs
  • Date: 2016-10-24 20:56:05 UTC
  • Revision ID: nicholas.skaggs@canonical.com-20161024205605-z8lta0uvuhtxwzwl
Initi with beta15

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// Copyright 2013 The Go Authors. All rights reserved.
 
2
// Use of this source code is governed by a BSD-style
 
3
// license that can be found in the LICENSE file.
 
4
 
 
5
// Pool is no-op under race detector, so all these tests do not work.
 
6
// +build !race
 
7
 
 
8
package mempool
 
9
 
 
10
import (
 
11
        "reflect"
 
12
        "sort"
 
13
        "testing"
 
14
)
 
15
 
 
16
// Note: the tests in this file are (largely) taken directly from
 
17
// $GOROOT/src/sync/pool_test.go, with the
 
18
// exception of all the GC-driven behaviour
 
19
// which has been removed.
 
20
 
 
21
func TestPool(t *testing.T) {
 
22
        var p Pool
 
23
        if p.Get() != nil {
 
24
                t.Fatal("expected empty")
 
25
        }
 
26
        p.Put("a")
 
27
        p.Put("b")
 
28
        // Note: we don't care what order the values come out
 
29
        // in - the sync implementation works like a queue
 
30
        // where the portable implementation works like a stack.
 
31
        g := make([]string, 2)
 
32
        g[0] = p.Get().(string)
 
33
        g[1] = p.Get().(string)
 
34
        sort.Strings(g)
 
35
        if want := []string{"a", "b"}; !reflect.DeepEqual(g, want) {
 
36
                t.Fatalf("got %q want %q", g, want)
 
37
        }
 
38
        if g := p.Get(); g != nil {
 
39
                t.Fatalf("got %#v; want nil", g)
 
40
        }
 
41
 
 
42
        p.Put("c")
 
43
}
 
44
 
 
45
func TestPoolNew(t *testing.T) {
 
46
        i := 0
 
47
        p := Pool{
 
48
                New: func() interface{} {
 
49
                        i++
 
50
                        return i
 
51
                },
 
52
        }
 
53
        if v := p.Get(); v != 1 {
 
54
                t.Fatalf("got %v; want 1", v)
 
55
        }
 
56
        if v := p.Get(); v != 2 {
 
57
                t.Fatalf("got %v; want 2", v)
 
58
        }
 
59
        p.Put(42)
 
60
        if v := p.Get(); v != 42 {
 
61
                t.Fatalf("got %v; want 42", v)
 
62
        }
 
63
        if v := p.Get(); v != 3 {
 
64
                t.Fatalf("got %v; want 3", v)
 
65
        }
 
66
}
 
67
 
 
68
func TestPoolStress(t *testing.T) {
 
69
        const P = 10
 
70
        N := int(1e6)
 
71
        if testing.Short() {
 
72
                N /= 100
 
73
        }
 
74
        var p Pool
 
75
        done := make(chan bool)
 
76
        for i := 0; i < P; i++ {
 
77
                go func() {
 
78
                        var v interface{} = 0
 
79
                        for j := 0; j < N; j++ {
 
80
                                if v == nil {
 
81
                                        v = 0
 
82
                                }
 
83
                                p.Put(v)
 
84
                                v = p.Get()
 
85
                                if v != nil && v.(int) != 0 {
 
86
                                        t.Fatalf("expect 0, got %v", v)
 
87
                                }
 
88
                        }
 
89
                        done <- true
 
90
                }()
 
91
        }
 
92
        for i := 0; i < P; i++ {
 
93
                <-done
 
94
        }
 
95
}