53 lines
1.1 KiB
Go
53 lines
1.1 KiB
Go
package buildrun
|
|
|
|
import (
|
|
"encoding/json"
|
|
"sync"
|
|
"testing"
|
|
"time"
|
|
|
|
"git.novox.be/novox/mesh-delivery/sdk/buildstep"
|
|
)
|
|
|
|
// keys is a steps state in memory: every value put, in order, per key, and a signal for each end.
|
|
type keys struct {
|
|
mu sync.Mutex
|
|
puts map[string][]buildstep.Step
|
|
ended chan string
|
|
}
|
|
|
|
func newKeys() *keys { return &keys{puts: map[string][]buildstep.Step{}, ended: make(chan string, 16)} }
|
|
|
|
func (k *keys) put(key string, value any) (uint64, error) {
|
|
step, err := buildstep.DecodeStep(value.(json.RawMessage))
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
k.mu.Lock()
|
|
k.puts[key] = append(k.puts[key], step)
|
|
n := uint64(len(k.puts[key]))
|
|
k.mu.Unlock()
|
|
if step.State != buildstep.StepRunning {
|
|
k.ended <- key
|
|
}
|
|
return n, nil
|
|
}
|
|
|
|
func (k *keys) last(key string) buildstep.Step {
|
|
k.mu.Lock()
|
|
defer k.mu.Unlock()
|
|
s := k.puts[key]
|
|
return s[len(s)-1]
|
|
}
|
|
|
|
func (k *keys) waitEnd(t *testing.T) string {
|
|
t.Helper()
|
|
select {
|
|
case key := <-k.ended:
|
|
return key
|
|
case <-time.After(3 * time.Second):
|
|
t.Fatal("the step never ended on its key")
|
|
return ""
|
|
}
|
|
}
|