Skip to content

Commit

Permalink
test(backend): fix flaky test
Browse files Browse the repository at this point in the history
  • Loading branch information
burdiyan committed Feb 27, 2024
1 parent f99f32d commit ed87a71
Showing 1 changed file with 29 additions and 50 deletions.
79 changes: 29 additions & 50 deletions backend/daemon/daemon_e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,17 +138,41 @@ func TestDaemonPushPublication(t *testing.T) {
}

func TestAPIGetRemotePublication(t *testing.T) {
t.Parallel()

ctx := context.Background()

// Carol will be the DHT server
dhtProvider := makeTestApp(t, "carol", makeTestConfig(t), true)
// Carol is the DHT node.
carol := makeTestApp(t, "carol", makeTestConfig(t), true)

var alice *App
{
cfg := makeTestConfig(t)
cfg.P2P.BootstrapPeers = carol.Net.MustGet().Libp2p().AddrsFull()
alice = makeTestApp(t, "alice", cfg, true)
}

var bob *App
{
cfg := makeTestConfig(t)
cfg.P2P.BootstrapPeers = carol.Net.MustGet().Libp2p().AddrsFull()
bob = makeTestApp(t, "bob", cfg, true)
}

// Make sure bob and alice don't know each other.
require.NoError(t, bob.Net.MustGet().Libp2p().Network().ClosePeer(alice.Storage.Device().ID()))
bob.Net.MustGet().Libp2p().Peerstore().RemovePeer(alice.Storage.Device().ID())
require.NoError(t, alice.Net.MustGet().Libp2p().Network().ClosePeer(bob.Storage.Device().ID()))
alice.Net.MustGet().Libp2p().Peerstore().RemovePeer(bob.Storage.Device().ID())

pub := publishDocument(t, ctx, alice)

requester, publishedDocument, _ := makeRemotePublication(t, ctx, dhtProvider)
time.Sleep(time.Second)

remotePublication, err := requester.RPC.Documents.GetPublication(ctx, &documents.GetPublicationRequest{DocumentId: publishedDocument.Document.Id})
remotePub, err := bob.RPC.Documents.GetPublication(ctx, &documents.GetPublicationRequest{DocumentId: pub.Document.Id})
require.NoError(t, err)

testutil.ProtoEqual(t, publishedDocument, remotePublication, "remote publication doesn't match")
testutil.ProtoEqual(t, pub, remotePub, "remote publication doesn't match")
}

func TestBug_SyncHangs(t *testing.T) {
Expand Down Expand Up @@ -362,32 +386,6 @@ func getAddrs(t *testing.T, a *App) []string {
return mttnet.AddrInfoToStrings(a.Net.MustGet().AddrInfo())
}

func makeRemotePublication(t *testing.T, ctx context.Context, dhtProvider *App) (*App, *documents.Publication, *App) {
var publisher *App
{
cfg := makeTestConfig(t)
cfg.P2P.BootstrapPeers = dhtProvider.Net.MustGet().Libp2p().AddrsFull()
publisher = makeTestApp(t, "alice", cfg, true)
}

var bob *App
{
cfg := makeTestConfig(t)
cfg.P2P.BootstrapPeers = dhtProvider.Net.MustGet().Libp2p().AddrsFull()
bob = makeTestApp(t, "bob", cfg, true)
}

// Make sure bob does't know anything about publisher.
require.NoError(t, bob.Net.MustGet().Libp2p().Network().ClosePeer(publisher.Storage.Device().ID()))
bob.Net.MustGet().Libp2p().Peerstore().RemovePeer(publisher.Storage.Device().ID())

publishedDocument := publishDocument(t, ctx, publisher)

// Sleeping just in case to make sure alices publication propagates.
time.Sleep(time.Second)
return bob, publishedDocument, publisher
}

func publishDocument(t *testing.T, ctx context.Context, publisher *App) *documents.Publication {
draft, err := publisher.RPC.Documents.CreateDraft(ctx, &documents.CreateDraftRequest{})
require.NoError(t, err)
Expand All @@ -410,22 +408,3 @@ func publishDocument(t *testing.T, ctx context.Context, publisher *App) *documen
require.NoError(t, err)
return published
}

func updateDocumenTitle(t *testing.T, ctx context.Context, publisher *App, docID, newTitle string) *documents.Publication {
draft, err := publisher.RPC.Documents.CreateDraft(ctx, &documents.CreateDraftRequest{
ExistingDocumentId: docID,
})
require.NoError(t, err)

updated, err := publisher.RPC.Documents.UpdateDraft(ctx, &documents.UpdateDraftRequest{
DocumentId: draft.Id,
Changes: []*documents.DocumentChange{
{Op: &documents.DocumentChange_SetTitle{SetTitle: newTitle}},
},
})
require.NoError(t, err)
require.NotNil(t, updated)
published, err := publisher.RPC.Documents.PublishDraft(ctx, &documents.PublishDraftRequest{DocumentId: draft.Id})
require.NoError(t, err)
return published
}

0 comments on commit ed87a71

Please sign in to comment.