A publish carrying an ask id is kept in the state taken for ask-taken-bound (10m): a repeat while the act runs joins it, a repeat after it ended answers the act's state read afresh (the transfer's lacking parts, a held version ended, a transfer gone refused), never publishing twice. Builds: design 47 H2 §775, §820; H-A-359, H-A-381
64 lines
2.3 KiB
Go
64 lines
2.3 KiB
Go
package main
|
|
|
|
import (
|
|
"errors"
|
|
"sync"
|
|
"sync/atomic"
|
|
"testing"
|
|
"time"
|
|
|
|
"distribution/internal/holder"
|
|
)
|
|
|
|
// A publish ask given twice acts once (design 47 H2 §775; H-A-381): a repeat while the act runs joins it and gets its
|
|
// answer, a repeat after it ended answers the act's state, a call with no ask id acts as before, a take that fails is said.
|
|
func TestAPublishAskIsTakenOnce(t *testing.T) {
|
|
taken := map[string]bool{}
|
|
var mu sync.Mutex
|
|
k := &asks{bound: time.Minute, take: func(seat, ask string, bound time.Duration) (bool, error) {
|
|
mu.Lock()
|
|
defer mu.Unlock()
|
|
if ask == "broken" {
|
|
return false, errors.New("no responders")
|
|
}
|
|
if bound != time.Minute || taken[seat+"."+ask] {
|
|
return false, nil
|
|
}
|
|
taken[seat+"."+ask] = true
|
|
return true, nil
|
|
}}
|
|
var acts, repeats atomic.Int32
|
|
release, started, joined := make(chan struct{}), make(chan struct{}, 1), make(chan struct{}, 1)
|
|
k.joined = func() { joined <- struct{}{} }
|
|
act := func() (holder.PublishAnswer, error) {
|
|
acts.Add(1)
|
|
started <- struct{}{}
|
|
<-release
|
|
return holder.PublishAnswer{Transfer: "t", Missing: []string{"d"}}, nil
|
|
}
|
|
repeat := func() (holder.PublishAnswer, error) { repeats.Add(1); return holder.PublishAnswer{Transfer: "t"}, nil }
|
|
answers := make(chan holder.PublishAnswer, 2)
|
|
go func() { a, _ := k.once(holder.ArtifactSeat, "a1", act, repeat); answers <- a }()
|
|
<-started
|
|
go func() { a, _ := k.once(holder.ArtifactSeat, "a1", act, repeat); answers <- a }()
|
|
<-joined
|
|
close(release)
|
|
for range 2 {
|
|
if a := <-answers; len(a.Missing) != 1 {
|
|
t.Errorf("an answer of the running act = %+v", a)
|
|
}
|
|
}
|
|
if a, _ := k.once(holder.ArtifactSeat, "a1", act, repeat); acts.Load() != 1 || repeats.Load() != 1 || a.Transfer != "t" {
|
|
t.Errorf("acts %d, repeats %d after a repeat of an ended act", acts.Load(), repeats.Load())
|
|
}
|
|
release = make(chan struct{})
|
|
close(release)
|
|
act2 := func() (holder.PublishAnswer, error) { acts.Add(1); return holder.PublishAnswer{}, nil }
|
|
if _, err := k.once(holder.ArtifactSeat, "", act2, repeat); err != nil || acts.Load() != 2 {
|
|
t.Errorf("a call with no ask id: acts %d, %v", acts.Load(), err)
|
|
}
|
|
if _, err := k.once(holder.ArtifactSeat, "broken", act2, repeat); err == nil || acts.Load() != 2 {
|
|
t.Errorf("a failed take: acts %d, %v", acts.Load(), err)
|
|
}
|
|
}
|