test for 'multi process' concurrency

This commit is contained in:
Niko Abeler 2022-09-11 21:22:59 +02:00
parent e9bcaa4c4a
commit 7a70be9839
1 changed files with 59 additions and 0 deletions

View File

@ -550,3 +550,62 @@ func TestComplexParallelWebmentions(t *testing.T) {
t.Errorf("Expected 20 webmentions, got %d", len(outs)) 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))
}
}