| OLD | NEW |
| (Empty) | |
| 1 // Copyright 2016 The LUCI Authors. All rights reserved. |
| 2 // Use of this source code is governed under the Apache License, Version 2.0 |
| 3 // that can be found in the LICENSE file. |
| 4 |
| 5 package bufferpool |
| 6 |
| 7 import ( |
| 8 "fmt" |
| 9 "testing" |
| 10 |
| 11 . "github.com/smartystreets/goconvey/convey" |
| 12 ) |
| 13 |
| 14 func TestBufferPool(t *testing.T) { |
| 15 t.Parallel() |
| 16 |
| 17 Convey(`Testing P, the buffer pool`, t, func() { |
| 18 var p P |
| 19 |
| 20 Convey(`Basic functionality`, func() { |
| 21 buf1 := p.Get() |
| 22 buf1.WriteString("foo") |
| 23 So(buf1.String(), ShouldEqual, "foo") |
| 24 |
| 25 clone1 := buf1.Clone() |
| 26 So(clone1, ShouldResemble, []byte("foo")) |
| 27 buf1.Release() |
| 28 |
| 29 buf2 := p.Get() |
| 30 buf2.WriteString("bar") |
| 31 |
| 32 // clone1 should still resemble "foo". |
| 33 So(clone1, ShouldResemble, []byte("foo")) |
| 34 }) |
| 35 |
| 36 Convey(`Double release panicks`, func() { |
| 37 buf := p.Get() |
| 38 buf.Release() |
| 39 So(buf.Release, ShouldPanic) |
| 40 }) |
| 41 |
| 42 Convey(`Concurrent access`, func() { |
| 43 const goroutines = 16 |
| 44 const rounds = 128 |
| 45 |
| 46 startC := make(chan struct{}) |
| 47 doneC := make(chan []byte) |
| 48 |
| 49 for i := 0; i < goroutines; i++ { |
| 50 go func(idx int) { |
| 51 <-startC |
| 52 |
| 53 for j := 0; j < rounds; j++ { |
| 54 buf := p.Get() |
| 55 fmt.Fprintf(buf, "%d.%d", idx, j
) |
| 56 doneC <- buf.Clone() |
| 57 buf.Release() |
| 58 } |
| 59 }(i) |
| 60 } |
| 61 |
| 62 // Collect all of our data. Store it as bytes so that if
there is |
| 63 // conflict / reuse, something will hopefully go wrong. |
| 64 close(startC) |
| 65 data := make([][]byte, 0, goroutines*rounds) |
| 66 for i := 0; i < goroutines; i++ { |
| 67 for j := 0; j < rounds; j++ { |
| 68 data = append(data, <-doneC) |
| 69 } |
| 70 } |
| 71 |
| 72 // Assert that it all exists. |
| 73 sorted := make(map[string]struct{}, len(data)) |
| 74 for _, d := range data { |
| 75 sorted[string(d)] = struct{}{} |
| 76 } |
| 77 So(len(sorted), ShouldEqual, goroutines*rounds) |
| 78 }) |
| 79 }) |
| 80 } |
| OLD | NEW |