Files
mesh-delivery/modules/mesh-delivery-indexer/internal/index/behind_test.go
T

113 lines
5.5 KiB
Go

package index
import (
"context"
"encoding/json"
"testing"
"time"
"git.novox.be/novox/mesh-delivery/modules/mesh-delivery-forge-bridge/forgemsg"
"git.novox.be/novox/mesh-delivery/modules/mesh-delivery-indexer/takein"
"git.novox.be/novox/mesh-delivery/modules/mesh-delivery-sdk/trunk"
)
// Design 47 G A-463: a merge the indexer never took in is noticed at a source revision and taken in as its merged message
// would have been, with a finding raised while it stays behind and cleared once it is taken in.
func TestAMergeTheIndexerNeverTookInIsTakenInAndSaidWhileItIsBehind(t *testing.T) {
ctx := context.Background()
b := newTakeInBed(t)
b.ix.Taken, b.ix.Findings = newMem(), newFindings()
key, _ := trunk.RepositoryKey(catalogueRepo)
old, head := sha('7'), sha('8')
data, _ := takein.EncodeTaken(takein.Taken{Repository: trunk.RepositoryName(catalogueRepo), Merge: old})
b.ix.Taken.(*memStore).put(key, data)
ancestry := map[[2]string]bool{{old, head}: true} // old is in head
b.ix.Contains = func(_ context.Context, _, commit, in string) (bool, error) {
return commit == in || ancestry[[2]string{commit, in}], nil
}
b.ix.source = sourceNow{rev: 3, heads: map[string]string{catalogueRepo: head}}
// The head has no merged message yet: nothing is taken in and the finding stays.
if done := b.ix.TakeInBehind(ctx); len(done) != 0 || len(b.ix.Findings.Held()) != 1 || b.ix.Findings.Held()[0].Kind != UntakenMergeKind {
t.Fatalf("a head whose message is not read: taken in %v, findings %+v: want none taken in and %s raised", done, b.ix.Findings.Held(), UntakenMergeKind)
}
// Its message is there: it is taken in, its taken key moves and the finding is cleared.
b.publish(t, b.merge(30, head, "tree-avahi-3", ""))
if done := b.ix.TakeInBehind(ctx); len(done) != 1 || len(b.ix.Findings.Held()) != 0 || b.commit(t, "avahi") != head {
t.Fatalf("a head with its message: taken in %v, findings %+v, avahi at %s: want it taken in at %s and the finding cleared", done, b.ix.Findings.Held(), b.commit(t, "avahi"), head)
}
if v, _, _, _ := b.ix.Taken.Get(ctx, key); string(v) == string(data) {
t.Errorf("taken.%s still names %s", key, old)
}
// A head the taken merge already holds (the source not yet moved to a merge just taken in) is left.
b.ix.source = sourceNow{rev: 4, heads: map[string]string{catalogueRepo: old}}
if done := b.ix.TakeInBehind(ctx); len(done) != 0 {
t.Errorf("a head older than the taken merge: taken in %v, want none", done)
}
// A source revision starts the comparison by itself (no timer): the taken key is put back to the old merge and the source
// names the head again.
b.ix.Taken.(*memStore).put(key, data)
if err := applyS(b.ix, ctx, 5, sourceOf(catalogueRepo, head)); err != nil {
t.Fatal(err)
}
b.ix.catchUpWG.Wait()
if v, _, _, _ := b.ix.Taken.Get(ctx, key); string(v) == string(data) {
t.Errorf("a source revision naming %s left taken.%s at %s", head, key, old)
}
}
// Design 47 G A-463a: a head the pending holds (a group member waiting for its group's end) is not behind: it raises no finding
// and is not taken in alone.
func TestAHeadThePendingHoldsIsNotBehind(t *testing.T) {
ctx := context.Background()
b := newTakeInBed(t)
b.ix.Taken, b.ix.Findings = newMem(), newFindings()
key, _ := trunk.RepositoryKey(catalogueRepo)
old, head := sha('7'), sha('8')
data, _ := takein.EncodeTaken(takein.Taken{Repository: trunk.RepositoryName(catalogueRepo), Merge: old})
b.ix.Taken.(*memStore).put(key, data)
b.ix.Contains = func(_ context.Context, _, commit, in string) (bool, error) { return commit == in || commit == old, nil }
b.publish(t, b.merge(30, head, "tree-avahi-3", ""))
// The head waits in pending as a member of a group not ended.
held := b.merge(30, head, "tree-avahi-3", "")
raw, _ := forgemsg.EncodeForge(held)
pv, _ := json.Marshal(Pending{V: 1, Forge: raw, Group: "feat", Since: time.Now(), LastWriter: "x"})
b.pending.put(takein.PendingKey(1), pv)
b.ix.source = sourceNow{rev: 3, heads: map[string]string{catalogueRepo: head}}
if done := b.ix.TakeInBehind(ctx); len(done) != 0 || len(b.ix.Findings.Held()) != 0 || b.commit(t, "avahi") == head {
t.Fatalf("a head the pending holds: taken in %v, findings %+v, avahi at %s: want none taken in and none raised", done, b.ix.Findings.Held(), b.commit(t, "avahi"))
}
}
// Design 47 G A-463a: a source revision heard while a catch-up runs is compared again when that catch-up ends.
func TestASourceRevisionHeardDuringACatchUpIsComparedWhenItEnds(t *testing.T) {
ctx := context.Background()
b := newTakeInBed(t)
b.ix.Taken, b.ix.Findings = newMem(), newFindings()
key, _ := trunk.RepositoryKey(catalogueRepo)
h1, h2 := sha('8'), sha('9')
b.ix.Contains = func(_ context.Context, _, commit, in string) (bool, error) {
return commit == in || (commit == h1 && in == h2), nil
}
b.publish(t, b.merge(30, h1, "tree-avahi-3", ""))
b.publish(t, b.merge(31, h2, "tree-avahi-4", ""))
b.ix.source = sourceNow{rev: 3, heads: map[string]string{catalogueRepo: h1}}
inner := b.ix.Message
heard := false
b.ix.Message = func(c context.Context, r, m string, n int) ([]byte, error) {
if !heard {
heard = true // the catch-up of h1 runs: a new source revision is heard now
if err := applyS(b.ix, ctx, 4, sourceOf(catalogueRepo, h2)); err != nil {
t.Error(err)
}
}
return inner(c, r, m, n)
}
b.ix.CatchUp(ctx)
b.ix.catchUpWG.Wait()
v, _, _, _ := b.ix.Taken.Get(ctx, key)
if got, _ := takein.DecodeTaken(v); got.Merge != h2 {
t.Errorf("taken after the catch-up = %s, want %s: the revision heard during it is compared afterwards\n%s", got.Merge, h2, b.logs)
}
}