107 lines
3.7 KiB
Go
107 lines
3.7 KiB
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"strings"
|
|
"sync"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
// The incoming store's steps (design 47 H2 §819: one test function for the bounded steps).
|
|
func TestTheBoundedSteps(t *testing.T) {
|
|
t.Run("a hung step answers with its name", hungStepAnswersWithItsName)
|
|
t.Run("a given-up step says when its call ended", givenUpStepSaysWhenItsCallEnded)
|
|
t.Run("a deleted chunk is purged", deletedChunkIsPurged)
|
|
t.Run("a finding is said off the path", findingIsSaidOffThePath)
|
|
}
|
|
|
|
// A bus operation that never answers becomes an error naming its step, within the bound.
|
|
func hungStepAnswersWithItsName(t *testing.T) {
|
|
block := make(chan struct{})
|
|
defer close(block)
|
|
_, err := step(10*time.Millisecond, "the write of the idle key", func() (int, error) { <-block; return 0, nil })
|
|
if err == nil || !strings.Contains(err.Error(), "the write of the idle key did not answer within") {
|
|
t.Fatalf("err = %v", err)
|
|
}
|
|
if v, err := step(time.Second, "x", func() (int, error) { return 7, nil }); v != 7 || err != nil {
|
|
t.Fatalf("%d %v", v, err)
|
|
}
|
|
}
|
|
|
|
// A step given up on is said given up by name, and the log says later when the call behind it ended and how (A-443a).
|
|
func givenUpStepSaysWhenItsCallEnded(t *testing.T) {
|
|
var mu sync.Mutex
|
|
var lines []string
|
|
ended := make(chan struct{})
|
|
w := func(f string, a ...any) {
|
|
l := fmt.Sprintf(f, a...)
|
|
mu.Lock()
|
|
lines = append(lines, l)
|
|
mu.Unlock()
|
|
if strings.Contains(l, "the put of a chunk") && strings.Contains(l, "its call ended after") {
|
|
close(ended)
|
|
}
|
|
}
|
|
old := logTo.Swap(&w)
|
|
defer logTo.Store(old)
|
|
release := make(chan struct{})
|
|
if _, err := step(10*time.Millisecond, "the put of a chunk", func() (int, error) { <-release; return 0, fmt.Errorf("bus closed") }); err == nil {
|
|
t.Fatal("not given up")
|
|
}
|
|
close(release)
|
|
<-ended
|
|
mu.Lock()
|
|
all := strings.Join(lines, "\n")
|
|
mu.Unlock()
|
|
if !strings.Contains(all, "given up") || !strings.Contains(all, "bus closed") {
|
|
t.Fatalf("log: %s", all)
|
|
}
|
|
}
|
|
|
|
// The finding is said off the caller's path: a writer that never answers does not hold the call that says it.
|
|
func findingIsSaidOffThePath(t *testing.T) {
|
|
block := make(chan struct{})
|
|
defer close(block)
|
|
say := boundedFinding(func(string, string, string, bool) { <-block })
|
|
done := make(chan struct{})
|
|
go func() {
|
|
say("incoming-full", "incoming", "w", false)
|
|
say("incoming-full", "incoming", "w", false)
|
|
close(done)
|
|
}()
|
|
select {
|
|
case <-done:
|
|
case <-time.After(5 * time.Second):
|
|
t.Fatal("the call was held by the finding")
|
|
}
|
|
}
|
|
|
|
// fakeObjects is an object store that counts deletes.
|
|
type fakeObjects struct{ deleted []string }
|
|
|
|
func (f *fakeObjects) Put(string, []byte) error { return nil }
|
|
func (f *fakeObjects) Get(string) ([]byte, error) { return nil, nil }
|
|
func (f *fakeObjects) Delete(name string) error { f.deleted = append(f.deleted, name); return nil }
|
|
func (f *fakeObjects) Used() (uint64, error) { return 0, nil }
|
|
|
|
// Every delete of a chunk purges what it left behind, so the store's kept descriptions equal its live objects (design 47
|
|
// H2 §813 (8)); a purge's failure is said, never swallowed.
|
|
func deletedChunkIsPurged(t *testing.T) {
|
|
f := &fakeObjects{}
|
|
var purged []string
|
|
b := boundedObjects{f, func(name string) error { purged = append(purged, name); return nil }}
|
|
for i := range 1000 {
|
|
if err := b.Delete(fmt.Sprintf("chunk.%d", i)); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
if len(f.deleted) != 1000 || len(purged) != 1000 || purged[999] != "chunk.999" {
|
|
t.Fatalf("deleted %d, purged %d", len(f.deleted), len(purged))
|
|
}
|
|
b.Purge = func(string) error { return fmt.Errorf("no responders") }
|
|
if err := b.Delete("chunk.x"); err == nil || !strings.Contains(err.Error(), "no responders") {
|
|
t.Fatalf("a failed purge answered %v", err)
|
|
}
|
|
}
|