mirror of https://github.com/tinygo-org/tinygo.git
Anuraag Agrawal
2 years ago
committed by
Ron Evans
2 changed files with 59 additions and 3 deletions
@ -0,0 +1,50 @@ |
|||
package sync_test |
|||
|
|||
import ( |
|||
"sync" |
|||
"testing" |
|||
) |
|||
|
|||
type testItem struct { |
|||
val int |
|||
} |
|||
|
|||
func TestPool(t *testing.T) { |
|||
p := sync.Pool{ |
|||
New: func() interface{} { |
|||
return &testItem{} |
|||
}, |
|||
} |
|||
|
|||
i1P := p.Get() |
|||
if i1P == nil { |
|||
t.Error("pool with New returned nil") |
|||
} |
|||
i1 := i1P.(*testItem) |
|||
if got, want := i1.val, 0; got != want { |
|||
t.Errorf("empty pool item value: got %v, want %v", got, want) |
|||
} |
|||
i1.val = 1 |
|||
|
|||
i2 := p.Get().(*testItem) |
|||
if got, want := i2.val, 0; got != want { |
|||
t.Errorf("empty pool item value: got %v, want %v", got, want) |
|||
} |
|||
i2.val = 2 |
|||
|
|||
p.Put(i1) |
|||
|
|||
i3 := p.Get().(*testItem) |
|||
if got, want := i3.val, 1; got != want { |
|||
t.Errorf("pool with item value: got %v, want %v", got, want) |
|||
} |
|||
} |
|||
|
|||
func TestPool_noNew(t *testing.T) { |
|||
p := sync.Pool{} |
|||
|
|||
i1 := p.Get() |
|||
if i1 != nil { |
|||
t.Errorf("pool without New returned %v, want nil", i1) |
|||
} |
|||
} |
Loading…
Reference in new issue