test(files): add mockClient
This commit is contained in:
parent
d293ed52cc
commit
a36ef5f98f
3 changed files with 133 additions and 174 deletions
|
@ -1,8 +1,6 @@
|
||||||
package server
|
package server
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
|
||||||
"fmt"
|
|
||||||
"os"
|
"os"
|
||||||
"sync"
|
"sync"
|
||||||
"testing"
|
"testing"
|
||||||
|
@ -31,7 +29,8 @@ func BenchmarkUploadAndDownload(b *testing.B) {
|
||||||
"host": "127.0.0.1"
|
"host": "127.0.0.1"
|
||||||
},
|
},
|
||||||
"fs": {
|
"fs": {
|
||||||
"root": "tmpTestData"
|
"root": "tmpTestData",
|
||||||
|
"opensLimit": 1024
|
||||||
},
|
},
|
||||||
"db": {
|
"db": {
|
||||||
"dbPath": "tmpTestData/quickshare"
|
"dbPath": "tmpTestData/quickshare"
|
||||||
|
@ -61,77 +60,31 @@ func BenchmarkUploadAndDownload(b *testing.B) {
|
||||||
userCount := 5
|
userCount := 5
|
||||||
userPwd := "1234"
|
userPwd := "1234"
|
||||||
users := addUsers(b, addr, userPwd, userCount, adminToken)
|
users := addUsers(b, addr, userPwd, userCount, adminToken)
|
||||||
|
filesCount := 30
|
||||||
|
rounds := 1
|
||||||
|
|
||||||
getFilePath := func(name string, i int) string {
|
clients := []*MockClient{}
|
||||||
return fmt.Sprintf("%s/files/home_file_%d", name, i)
|
|
||||||
}
|
|
||||||
|
|
||||||
filesCount := 10
|
|
||||||
clientErrs := []error{}
|
|
||||||
mockClient := func(name, pwd string, wg *sync.WaitGroup) {
|
|
||||||
defer wg.Done()
|
|
||||||
|
|
||||||
userUsersCli := client.NewUsersClient(addr)
|
|
||||||
resp, _, errs := userUsersCli.Login(name, pwd)
|
|
||||||
if len(errs) > 0 {
|
|
||||||
clientErrs = append(clientErrs, errs...)
|
|
||||||
return
|
|
||||||
} else if resp.StatusCode != 200 {
|
|
||||||
clientErrs = append(clientErrs, fmt.Errorf("failed to login"))
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
files := map[string]string{}
|
|
||||||
content := "12345678"
|
|
||||||
for i := range make([]int, filesCount, filesCount) {
|
|
||||||
files[getFilePath(name, i)] = content
|
|
||||||
}
|
|
||||||
|
|
||||||
userToken := userUsersCli.Token()
|
|
||||||
for filePath, content := range files {
|
|
||||||
assertUploadOK(b, filePath, content, addr, userToken)
|
|
||||||
assertDownloadOK(b, filePath, content, addr, userToken)
|
|
||||||
}
|
|
||||||
|
|
||||||
filesCl := client.NewFilesClient(addr, userToken)
|
|
||||||
resp, lsResp, errs := filesCl.ListHome()
|
|
||||||
if len(errs) > 0 {
|
|
||||||
clientErrs = append(clientErrs, errs...)
|
|
||||||
return
|
|
||||||
} else if resp.StatusCode != 200 {
|
|
||||||
clientErrs = append(clientErrs, errors.New("failed to list home"))
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
if lsResp.Cwd != fmt.Sprintf("%s/files", name) {
|
|
||||||
clientErrs = append(clientErrs, fmt.Errorf("incorrct cwd (%s)", lsResp.Cwd))
|
|
||||||
return
|
|
||||||
|
|
||||||
} else if len(lsResp.Metadatas) != len(files) {
|
|
||||||
clientErrs = append(clientErrs, fmt.Errorf("incorrct metadata size (%d)", len(lsResp.Metadatas)))
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
resp, _, errs = filesCl.Delete(getFilePath(name, 0))
|
|
||||||
if len(errs) > 0 {
|
|
||||||
clientErrs = append(clientErrs, errs...)
|
|
||||||
return
|
|
||||||
} else if resp.StatusCode != 200 {
|
|
||||||
clientErrs = append(clientErrs, errors.New("failed to add user"))
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
for i := 0; i < b.N; i++ {
|
for i := 0; i < b.N; i++ {
|
||||||
|
for j := 0; j < rounds; j++ {
|
||||||
var wg sync.WaitGroup
|
var wg sync.WaitGroup
|
||||||
for userName := range users {
|
for userName := range users {
|
||||||
|
client := &MockClient{errs: []error{}}
|
||||||
|
clients = append(clients, client)
|
||||||
wg.Add(1)
|
wg.Add(1)
|
||||||
go mockClient(userName, userPwd, &wg)
|
go client.uploadAndDownload(b, addr, userName, userPwd, filesCount, &wg)
|
||||||
}
|
}
|
||||||
|
|
||||||
wg.Wait()
|
wg.Wait()
|
||||||
if len(clientErrs) > 0 {
|
|
||||||
b.Fatal(joinErrs(clientErrs))
|
errs := []error{}
|
||||||
|
for _, client := range clients {
|
||||||
|
if len(client.errs) > 0 {
|
||||||
|
errs = append(errs, client.errs...)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if len(errs) > 0 {
|
||||||
|
b.Fatal(joinErrs(errs))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,8 +1,6 @@
|
||||||
package server
|
package server
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
|
||||||
"fmt"
|
|
||||||
"os"
|
"os"
|
||||||
"sync"
|
"sync"
|
||||||
"testing"
|
"testing"
|
||||||
|
@ -61,113 +59,28 @@ func TestConcurrency(t *testing.T) {
|
||||||
userCount := 5
|
userCount := 5
|
||||||
userPwd := "1234"
|
userPwd := "1234"
|
||||||
users := addUsers(t, addr, userPwd, userCount, adminToken)
|
users := addUsers(t, addr, userPwd, userCount, adminToken)
|
||||||
|
|
||||||
getFilePath := func(name string, i int) string {
|
|
||||||
return fmt.Sprintf("%s/files/home_file_%d", name, i)
|
|
||||||
}
|
|
||||||
|
|
||||||
filesCount := 10
|
filesCount := 10
|
||||||
clientErrs := []error{}
|
|
||||||
mockClient := func(name, pwd string, wg *sync.WaitGroup) {
|
|
||||||
defer wg.Done()
|
|
||||||
|
|
||||||
userUsersCli := client.NewUsersClient(addr)
|
|
||||||
resp, _, errs := userUsersCli.Login(name, pwd)
|
|
||||||
if len(errs) > 0 {
|
|
||||||
clientErrs = append(clientErrs, errs...)
|
|
||||||
return
|
|
||||||
} else if resp.StatusCode != 200 {
|
|
||||||
clientErrs = append(clientErrs, fmt.Errorf("failed to login"))
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
files := map[string]string{}
|
|
||||||
content := "12345678"
|
|
||||||
for i := range make([]int, filesCount, filesCount) {
|
|
||||||
files[getFilePath(name, i)] = content
|
|
||||||
}
|
|
||||||
|
|
||||||
userToken := userUsersCli.Token()
|
|
||||||
for filePath, content := range files {
|
|
||||||
assertUploadOK(t, filePath, content, addr, userToken)
|
|
||||||
assertDownloadOK(t, filePath, content, addr, userToken)
|
|
||||||
}
|
|
||||||
|
|
||||||
filesCl := client.NewFilesClient(addr, userToken)
|
|
||||||
resp, lsResp, errs := filesCl.ListHome()
|
|
||||||
if len(errs) > 0 {
|
|
||||||
clientErrs = append(clientErrs, errs...)
|
|
||||||
return
|
|
||||||
} else if resp.StatusCode != 200 {
|
|
||||||
clientErrs = append(clientErrs, errors.New("failed to list home"))
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
if lsResp.Cwd != fmt.Sprintf("%s/files", name) {
|
|
||||||
clientErrs = append(clientErrs, fmt.Errorf("incorrct cwd (%s)", lsResp.Cwd))
|
|
||||||
return
|
|
||||||
|
|
||||||
} else if len(lsResp.Metadatas) != len(files) {
|
|
||||||
clientErrs = append(clientErrs, fmt.Errorf("incorrct metadata size (%d)", len(lsResp.Metadatas)))
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
resp, selfResp, errs := userUsersCli.Self()
|
|
||||||
if len(errs) > 0 {
|
|
||||||
clientErrs = append(clientErrs, errs...)
|
|
||||||
return
|
|
||||||
} else if resp.StatusCode != 200 {
|
|
||||||
clientErrs = append(clientErrs, errors.New("failed to self"))
|
|
||||||
return
|
|
||||||
}
|
|
||||||
if selfResp.UsedSpace != int64(filesCount*len(content)) {
|
|
||||||
clientErrs = append(
|
|
||||||
clientErrs,
|
|
||||||
fmt.Errorf("usedSpace(%d) doesn't match (%d)", selfResp.UsedSpace, filesCount*len(content)),
|
|
||||||
)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
resp, _, errs = filesCl.Delete(getFilePath(name, 0))
|
|
||||||
if len(errs) > 0 {
|
|
||||||
clientErrs = append(clientErrs, errs...)
|
|
||||||
return
|
|
||||||
} else if resp.StatusCode != 200 {
|
|
||||||
clientErrs = append(clientErrs, errors.New("failed to add user"))
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
resp, selfResp, errs = userUsersCli.Self()
|
|
||||||
if len(errs) > 0 {
|
|
||||||
clientErrs = append(clientErrs, errs...)
|
|
||||||
return
|
|
||||||
} else if resp.StatusCode != 200 {
|
|
||||||
clientErrs = append(clientErrs, errors.New("failed to self"))
|
|
||||||
return
|
|
||||||
}
|
|
||||||
if selfResp.UsedSpace != int64((filesCount-1)*len(content)) {
|
|
||||||
clientErrs = append(
|
|
||||||
clientErrs,
|
|
||||||
fmt.Errorf(
|
|
||||||
"usedSpace(%d) doesn't match (%d)",
|
|
||||||
selfResp.UsedSpace,
|
|
||||||
int64((filesCount-1)*len(content)),
|
|
||||||
),
|
|
||||||
)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
var wg sync.WaitGroup
|
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 userName := range users {
|
for userName := range users {
|
||||||
|
client := &MockClient{errs: []error{}}
|
||||||
|
clients = append(clients, client)
|
||||||
wg.Add(1)
|
wg.Add(1)
|
||||||
go mockClient(userName, userPwd, &wg)
|
go client.uploadAndDownload(t, addr, userName, userPwd, filesCount, &wg)
|
||||||
}
|
}
|
||||||
|
|
||||||
wg.Wait()
|
wg.Wait()
|
||||||
if len(clientErrs) > 0 {
|
|
||||||
t.Fatal(joinErrs(clientErrs))
|
errs := []error{}
|
||||||
|
for _, client := range clients {
|
||||||
|
if len(client.errs) > 0 {
|
||||||
|
errs = append(errs, client.errs...)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if len(errs) > 0 {
|
||||||
|
t.Fatal(joinErrs(errs))
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
|
@ -10,6 +10,7 @@ import (
|
||||||
"os"
|
"os"
|
||||||
"path"
|
"path"
|
||||||
"strings"
|
"strings"
|
||||||
|
"sync"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
@ -119,12 +120,12 @@ func assertUploadOK(t testing.TB, filePath, content, addr string, token *http.Co
|
||||||
cl := client.NewFilesClient(addr, token)
|
cl := client.NewFilesClient(addr, token)
|
||||||
|
|
||||||
fileSize := int64(len([]byte(content)))
|
fileSize := int64(len([]byte(content)))
|
||||||
res, _, errs := cl.Create(filePath, fileSize)
|
res, body, errs := cl.Create(filePath, fileSize)
|
||||||
if len(errs) > 0 {
|
if len(errs) > 0 {
|
||||||
t.Fatal(errs)
|
t.Fatal(errs)
|
||||||
return false
|
return false
|
||||||
} else if res.StatusCode != 200 {
|
} else if res.StatusCode != 200 {
|
||||||
t.Fatal(res.StatusCode)
|
t.Fatalf("unexpected code in upload(%d): %s", res.StatusCode, body)
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -172,7 +173,7 @@ func assertDownloadOK(t testing.TB, filePath, content, addr string, token *http.
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
if res.StatusCode != 200 && res.StatusCode != 206 {
|
if res.StatusCode != 200 && res.StatusCode != 206 {
|
||||||
t.Error(res.StatusCode)
|
t.Error(fmt.Errorf("error code is not 200 or 206 in download (%d): %s", res.StatusCode, body))
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
if contentDispositionHeader != fmt.Sprintf(`attachment; filename="%s"`, fileName) {
|
if contentDispositionHeader != fmt.Sprintf(`attachment; filename="%s"`, fileName) {
|
||||||
|
@ -245,3 +246,95 @@ func loginFilesClient(addr, user, pwd string) (*client.FilesClient, error) {
|
||||||
token := client.GetCookie(resp.Cookies(), q.TokenCookie)
|
token := client.GetCookie(resp.Cookies(), q.TokenCookie)
|
||||||
return client.NewFilesClient(addr, token), nil
|
return client.NewFilesClient(addr, token), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type MockClient struct {
|
||||||
|
errs []error
|
||||||
|
}
|
||||||
|
|
||||||
|
func (cl *MockClient) uploadAndDownload(tb testing.TB, addr, name, pwd string, filesCount int, wg *sync.WaitGroup) {
|
||||||
|
getFilePath := func(name string, i int) string {
|
||||||
|
return fmt.Sprintf("%s/files/home_file_%d", name, i)
|
||||||
|
}
|
||||||
|
|
||||||
|
defer wg.Done()
|
||||||
|
|
||||||
|
userUsersCli := client.NewUsersClient(addr)
|
||||||
|
resp, _, errs := userUsersCli.Login(name, pwd)
|
||||||
|
if len(errs) > 0 {
|
||||||
|
cl.errs = append(cl.errs, errs...)
|
||||||
|
return
|
||||||
|
} else if resp.StatusCode != 200 {
|
||||||
|
cl.errs = append(cl.errs, fmt.Errorf("failed to login"))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
files := map[string]string{}
|
||||||
|
content := "12345678"
|
||||||
|
for i := range make([]int, filesCount, filesCount) {
|
||||||
|
files[getFilePath(name, i)] = content
|
||||||
|
}
|
||||||
|
|
||||||
|
userToken := userUsersCli.Token()
|
||||||
|
for filePath, content := range files {
|
||||||
|
assertUploadOK(tb, filePath, content, addr, userToken)
|
||||||
|
assertDownloadOK(tb, filePath, content, addr, userToken)
|
||||||
|
}
|
||||||
|
|
||||||
|
filesCl := client.NewFilesClient(addr, userToken)
|
||||||
|
resp, lsResp, errs := filesCl.ListHome()
|
||||||
|
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 list home"))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if lsResp.Cwd != fmt.Sprintf("%s/files", name) {
|
||||||
|
cl.errs = append(cl.errs, fmt.Errorf("incorrct cwd (%s)", lsResp.Cwd))
|
||||||
|
return
|
||||||
|
|
||||||
|
} else if len(lsResp.Metadatas) != len(files) {
|
||||||
|
cl.errs = append(cl.errs, fmt.Errorf("incorrct metadata size (%d)", len(lsResp.Metadatas)))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
resp, selfResp, errs := userUsersCli.Self()
|
||||||
|
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 self"))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if selfResp.UsedSpace != int64(filesCount*len(content)) {
|
||||||
|
cl.errs = append(cl.errs, fmt.Errorf("usedSpace(%d) doesn't match (%d)", selfResp.UsedSpace, filesCount*len(content)))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
resp, _, errs = filesCl.Delete(getFilePath(name, 0))
|
||||||
|
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
|
||||||
|
}
|
||||||
|
|
||||||
|
resp, selfResp, errs = userUsersCli.Self()
|
||||||
|
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 self"))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if selfResp.UsedSpace != int64((filesCount-1)*len(content)) {
|
||||||
|
cl.errs = append(cl.errs, fmt.Errorf(
|
||||||
|
"usedSpace(%d) doesn't match (%d)",
|
||||||
|
selfResp.UsedSpace,
|
||||||
|
int64((filesCount-1)*len(content)),
|
||||||
|
))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue