test(server: add rounds for concurrency test

This commit is contained in:
hexxa 2022-08-26 20:01:33 +08:00 committed by Hexxa
parent a36ef5f98f
commit d0a8a6d43a
2 changed files with 35 additions and 18 deletions

View file

@ -56,31 +56,36 @@ func TestConcurrency(t *testing.T) {
} }
adminToken := client.GetCookie(resp.Cookies(), q.TokenCookie) adminToken := client.GetCookie(resp.Cookies(), q.TokenCookie)
userCount := 5 userCount := 2
userPwd := "1234" userPwd := "1234"
users := addUsers(t, addr, userPwd, userCount, adminToken) users := addUsers(t, addr, userPwd, userCount, adminToken)
filesCount := 10 filesCount := 5
rounds := 2
var wg sync.WaitGroup
t.Run("Upload and download concurrently", func(t *testing.T) { t.Run("Upload and download concurrently", func(t *testing.T) {
clients := []*MockClient{} for i := 0; i < rounds; i++ {
for userName := range users { clients := []*MockClient{}
client := &MockClient{errs: []error{}} var wg sync.WaitGroup
clients = append(clients, client)
wg.Add(1)
go client.uploadAndDownload(t, addr, userName, userPwd, filesCount, &wg)
}
wg.Wait() for userName := range users {
client := &MockClient{errs: []error{}}
clients = append(clients, client)
wg.Add(1)
go client.uploadAndDownload(t, addr, userName, userPwd, filesCount, &wg)
}
errs := []error{} wg.Wait()
for _, client := range clients {
if len(client.errs) > 0 { errs := []error{}
errs = append(errs, client.errs...) for _, client := range clients {
if len(client.errs) > 0 {
errs = append(errs, client.errs...)
}
}
if len(errs) > 0 {
t.Fatal(joinErrs(errs))
} }
} }
if len(errs) > 0 {
t.Fatal(joinErrs(errs))
}
}) })
} }

View file

@ -337,4 +337,16 @@ func (cl *MockClient) uploadAndDownload(tb testing.TB, addr, name, pwd string, f
)) ))
return return
} }
// truncate all files
for i := 1; i < filesCount; i++ {
resp, _, errs = filesCl.Delete(getFilePath(name, i))
if len(errs) > 0 {
cl.errs = append(cl.errs, errs...)
return
} else if resp.StatusCode != 200 {
cl.errs = append(cl.errs, errors.New("failed to delete file"))
return
}
}
} }