From 7a70be98393acb56da102ebebfffd5ea28724b0c Mon Sep 17 00:00:00 2001 From: Niko Abeler Date: Sun, 11 Sep 2022 21:22:59 +0200 Subject: [PATCH] test for 'multi process' concurrency --- post_test.go | 59 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) diff --git a/post_test.go b/post_test.go index a2217dc..d9c087c 100644 --- a/post_test.go +++ b/post_test.go @@ -550,3 +550,62 @@ func TestComplexParallelWebmentions(t *testing.T) { t.Errorf("Expected 20 webmentions, got %d", len(outs)) } } +func TestComplexParallelSimulatedProcessesWebmentions(t *testing.T) { + repoName := testRepoName() + repo, _ := owl.CreateRepository(repoName, owl.RepoConfig{}) + repo.HttpClient = &MockHttpClient{} + repo.Parser = &MockParseLinksHtmlParser{ + Links: []string{ + "http://example.com/1", + "http://example.com/2", + "http://example.com/3", + }, + } + user, _ := repo.CreateUser("testuser") + post, _ := user.CreateNewPost("testpost") + + wg := sync.WaitGroup{} + wg.Add(40) + + for i := 0; i < 20; i++ { + go func(k int) { + defer wg.Done() + fRepo, _ := owl.OpenRepository(repoName) + fUser, _ := fRepo.GetUser("testuser") + fPost, err := fUser.GetPost(post.Id()) + if err != nil { + t.Error(err) + return + } + fPost.AddIncomingWebmention("http://example.com/" + strconv.Itoa(k)) + }(i) + go func(k int) { + defer wg.Done() + fRepo, _ := owl.OpenRepository(repoName) + fUser, _ := fRepo.GetUser("testuser") + fPost, err := fUser.GetPost(post.Id()) + if err != nil { + t.Error(err) + return + } + webmention := owl.WebmentionOut{ + Target: "http://example.com/" + strconv.Itoa(k), + } + fPost.SendWebmention(webmention) + }(i) + } + + wg.Wait() + + ins := post.IncomingWebmentions() + + if len(ins) != 20 { + t.Errorf("Expected 20 webmentions, got %d", len(ins)) + } + + outs := post.OutgoingWebmentions() + + if len(outs) != 20 { + t.Errorf("Expected 20 webmentions, got %d", len(outs)) + } +}