Skip to content

Commit

Permalink
local: unit-test the atomicity
Browse files Browse the repository at this point in the history
  • Loading branch information
urisimchoni committed Apr 30, 2020
1 parent d4cd0aa commit 5576ab7
Showing 1 changed file with 80 additions and 0 deletions.
80 changes: 80 additions & 0 deletions local/container_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package local_test

import (
"fmt"
"io"
"strings"
"testing"

Expand Down Expand Up @@ -60,3 +61,82 @@ func TestItemsPaging(t *testing.T) {
is.Equal(err, stow.ErrBadCursor)

}

type chunkOfZero int

func (c *chunkOfZero) Read(b []byte) (n int, err error) {
if *c <= 0 {
return 0, io.EOF
}
toFill := len(b)
if toFill > int(*c) {
toFill = int(*c)
}
leftBytes := int(*c) - toFill
*c = chunkOfZero(leftBytes)
for i := 0; i < toFill; i++ {
b[i] = 0
}
return toFill, nil
}

func TestAtomicWrites(t *testing.T) {
is := is.New(t)
testDir, teardown, err := setup()
is.NoErr(err)
defer teardown()
cfg := stow.ConfigMap{"path": testDir}
l, err := stow.Dial(local.Kind, cfg)
is.NoErr(err)
is.OK(l)

container, err := l.Container("one")
is.NoErr(err)
is.OK(container)

initialVer := chunkOfZero(1000000)
initialItem, err := container.Put("test-file", &initialVer, 1000000, nil)
is.NoErr(err)
sz, err := initialItem.Size()
is.NoErr(err)
is.True(sz == 1000000)

end := make(chan error)
go func() {
for i := 0; i < 10; i++ {
c := chunkOfZero(1000000)
_, err := container.Put("test-file", &c, 1000000, nil)
if err != nil {
end <- err
}
}

end <- nil
}()

loop:
for {
items, _, err := container.Items("", "", 10)
is.NoErr(err)
is.True(len(items) < 10)
found := false
for _, i := range items {
if i.Name() != "test-file" {
continue
}

found = true
sz, err := i.Size()
is.NoErr(err)
is.True(sz == 1000000)
}
is.True(found)

select {
case err := <-end:
is.NoErr(err)
break loop
default:
}
}
}

0 comments on commit 5576ab7

Please sign in to comment.