Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Test publish after remove for the context id #419

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 56 additions & 0 deletions engine/engine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -734,6 +734,62 @@ func TestEngine_RegenrateEntryChunksFromOldDatastore(t *testing.T) {
requireLoadEntryChunkFromEngine(t, subject, ad.Entries)
}

func TestEngine_DeleteAndPublishWithTheSameContextID(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), testTimeout)
t.Cleanup(cancel)

mhs1 := test.RandomMultihashes(42)
mhs2 := test.RandomMultihashes(42)

subject, err := engine.New()
require.NoError(t, err)
err = subject.Start(ctx)
require.NoError(t, err)
defer subject.Shutdown()

cnt := 0
subject.RegisterMultihashLister(func(ctx context.Context, p peer.ID, contextID []byte) (provider.MultihashIterator, error) {
cnt++
if cnt == 1 {
return provider.SliceMultihashIterator(mhs1), nil
} else {
// return mhs1 only for the first ad
return provider.SliceMultihashIterator(mhs2), nil
}
})

md := metadata.Default.New(metadata.Bitswap{})
contextId := []byte("fish")
paddr := peer.AddrInfo{ID: subject.Host().ID(), Addrs: subject.Host().Addrs()}

// publish the first ad
ad1Cid, err := subject.NotifyPut(ctx, &paddr, contextId, md)
require.NoError(t, err)
require.NotEqual(t, cid.Undef, ad1Cid)

// publish a remove for the context id followed by a new ad - that should result into the latest ad linking to the new array of multihashes
ad2Cid, err := subject.NotifyRemove(ctx, paddr.ID, contextId)
require.NoError(t, err)
require.NotEqual(t, ad1Cid, ad2Cid)

ad3Cid, err := subject.NotifyPut(ctx, &paddr, contextId, md)
require.NoError(t, err)
require.NotEqual(t, ad2Cid, ad3Cid)

latestAdCid, latestAd, err := subject.GetLatestAdv(ctx)
require.NoError(t, err)
require.Equal(t, ad3Cid, latestAdCid)

// read cached entries and extract multihashes
var mhs []multihash.Multihash
entryChunks := requireLoadEntryChunkFromEngine(t, subject, latestAd.Entries)
for _, ch := range entryChunks {
mhs = append(mhs, ch.Entries...)
}

require.Equal(t, mhs2, mhs)
}

func verifyAd(t *testing.T, ctx context.Context, subject *engine.Engine, expected, actual *schema.Advertisement) {
require.Equal(t, expected.ContextID, actual.ContextID)
require.Equal(t, expected.Provider, actual.Provider)
Expand Down
Loading