| 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(`Concurrent access`, func() { |
| 37 const goroutines = 16 |
| 38 const rounds = 128 |
| 39 |
| 40 startC := make(chan struct{}) |
| 41 doneC := make(chan []byte) |
| 42 |
| 43 for i := 0; i < goroutines; i++ { |
| 44 go func(idx int) { |
| 45 <-startC |
| 46 |
| 47 for j := 0; j < rounds; j++ { |
| 48 buf := p.Get() |
| 49 fmt.Fprintf(buf, "%d.%d", idx, j
) |
| 50 doneC <- buf.Clone() |
| 51 buf.Release() |
| 52 } |
| 53 }(i) |
| 54 } |
| 55 |
| 56 // Collect all of our data. Store it as bytes so that if
there is |
| 57 // conflict / reuse, something will hopefully go wrong. |
| 58 close(startC) |
| 59 data := make([][]byte, 0, goroutines*rounds) |
| 60 for i := 0; i < goroutines; i++ { |
| 61 for j := 0; j < rounds; j++ { |
| 62 data = append(data, <-doneC) |
| 63 } |
| 64 } |
| 65 |
| 66 // Assert that it all exists. |
| 67 sorted := make(map[string]struct{}, len(data)) |
| 68 for _, d := range data { |
| 69 sorted[string(d)] = struct{}{} |
| 70 } |
| 71 So(len(sorted), ShouldEqual, goroutines*rounds) |
| 72 }) |
| 73 }) |
| 74 } |
| OLD | NEW |