21448: expanded string constants Arvados-DCO-1.1-Signed-off-by: Lisa Knox <lisa.knox...
[arvados.git] / services / keepstore / streamwriterat_test.go
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 package keepstore
6
7 import (
8         "bytes"
9         "sync"
10
11         . "gopkg.in/check.v1"
12 )
13
14 var _ = Suite(&streamWriterAtSuite{})
15
16 type streamWriterAtSuite struct{}
17
18 func (s *streamWriterAtSuite) TestPartSizes(c *C) {
19         for partsize := 1; partsize < 5; partsize++ {
20                 for writesize := 1; writesize < 5; writesize++ {
21                         for datasize := 1; datasize < 100; datasize += 13 {
22                                 for bufextra := 0; bufextra < 5; bufextra++ {
23                                         c.Logf("=== partsize %d writesize %d datasize %d bufextra %d", partsize, writesize, datasize, bufextra)
24                                         outbuf := bytes.NewBuffer(nil)
25                                         indata := make([]byte, datasize)
26                                         for i := range indata {
27                                                 indata[i] = byte(i)
28                                         }
29                                         swa := newStreamWriterAt(outbuf, partsize, make([]byte, datasize+bufextra))
30                                         var wg sync.WaitGroup
31                                         for pos := 0; pos < datasize; pos += writesize {
32                                                 pos := pos
33                                                 wg.Add(1)
34                                                 go func() {
35                                                         defer wg.Done()
36                                                         endpos := pos + writesize
37                                                         if endpos > datasize {
38                                                                 endpos = datasize
39                                                         }
40                                                         swa.WriteAt(indata[pos:endpos], int64(pos))
41                                                 }()
42                                         }
43                                         wg.Wait()
44                                         swa.Close()
45                                         c.Check(outbuf.Bytes(), DeepEquals, indata)
46                                 }
47                         }
48                 }
49         }
50 }
51
52 func (s *streamWriterAtSuite) TestOverflow(c *C) {
53         for offset := -1; offset < 2; offset++ {
54                 buf := make([]byte, 50)
55                 swa := newStreamWriterAt(bytes.NewBuffer(nil), 20, buf)
56                 _, err := swa.WriteAt([]byte("foo"), int64(len(buf)+offset))
57                 c.Check(err, NotNil)
58                 err = swa.Close()
59                 c.Check(err, IsNil)
60         }
61 }
62
63 func (s *streamWriterAtSuite) TestIncompleteWrite(c *C) {
64         for _, partsize := range []int{20, 25} {
65                 for _, bufsize := range []int{50, 55, 60} {
66                         for offset := 0; offset < 3; offset++ {
67                                 swa := newStreamWriterAt(bytes.NewBuffer(nil), partsize, make([]byte, bufsize))
68                                 _, err := swa.WriteAt(make([]byte, 1), 49)
69                                 c.Check(err, IsNil)
70                                 _, err = swa.WriteAt(make([]byte, 46), int64(offset))
71                                 c.Check(err, IsNil)
72                                 err = swa.Close()
73                                 c.Check(err, NotNil)
74                                 c.Check(swa.WroteAt(), Equals, 47)
75                                 if offset == 0 {
76                                         c.Check(swa.Wrote(), Equals, 40/partsize*partsize)
77                                 } else {
78                                         c.Check(swa.Wrote(), Equals, 0)
79                                 }
80                         }
81                 }
82         }
83 }