From 6f9c87850abce8f1d22e1359652263dfa81e16f1 Mon Sep 17 00:00:00 2001 From: Dmitry Verkhoturov Date: Wed, 1 Jan 2020 21:46:39 +0100 Subject: [PATCH] Adjust tests timouts to work on machine with HDD (#516) * increase timeout for TestServerAuthHooks http client * replace assert.Equal checks for slice length with require.Equal * unify channel name across tests * fix panic in Test_Main * increase TestRest_CreateWithPictures timeout for HDD slowness * increase TestService_VoteSameIPWithDuration timeout for HDD slowness * increase go test timeout for HDD run * increase TestRest_CreateWithPictures timeout for HDD slowness --- Dockerfile | 2 +- backend/app/cmd/server_test.go | 6 +-- backend/app/main_test.go | 9 ++-- backend/app/migrator/backup_test.go | 3 +- backend/app/migrator/disqus_test.go | 4 +- backend/app/migrator/native_test.go | 8 ++-- backend/app/migrator/wordpress_test.go | 9 ++-- backend/app/notify/notify_test.go | 7 +-- backend/app/rest/api/admin_test.go | 14 +++--- backend/app/rest/api/rest_private_test.go | 4 +- backend/app/rest/api/rest_public_test.go | 16 +++---- backend/app/rest/api/rest_test.go | 6 +-- backend/app/store/engine/bolt_test.go | 44 +++++++++---------- backend/app/store/image/image_test.go | 4 +- backend/app/store/service/service_test.go | 52 +++++++++++------------ 15 files changed, 97 insertions(+), 91 deletions(-) diff --git a/Dockerfile b/Dockerfile index cfb8cbd4..290cc208 100644 --- a/Dockerfile +++ b/Dockerfile @@ -20,7 +20,7 @@ ENV GOFLAGS="-mod=vendor" RUN \ cd app && \ if [ -z "$SKIP_BACKEND_TEST" ] ; then \ - go test -p 1 -timeout="${BACKEND_TEST_TIMEOUT:-30s}" -covermode=count -coverprofile=/profile.cov_tmp ./... && \ + go test -p 1 -timeout="${BACKEND_TEST_TIMEOUT:-300s}" -covermode=count -coverprofile=/profile.cov_tmp ./... && \ cat /profile.cov_tmp | grep -v "_mock.go" > /profile.cov ; \ golangci-lint run --out-format=tab --disable-all --tests=false --enable=unconvert \ --enable=megacheck --enable=structcheck --enable=gas --enable=gocyclo --enable=dupl --enable=misspell \ diff --git a/backend/app/cmd/server_test.go b/backend/app/cmd/server_test.go index 26894f36..1f9fbda9 100644 --- a/backend/app/cmd/server_test.go +++ b/backend/app/cmd/server_test.go @@ -75,7 +75,7 @@ func TestServerApp_DevMode(t *testing.T) { go func() { _ = app.run(ctx) }() waitForHTTPServerStart(port) - assert.Equal(t, 5+1, len(app.restSrv.Authenticator.Providers()), "extra auth provider") + require.Equal(t, 5+1, len(app.restSrv.Authenticator.Providers()), "extra auth provider") assert.Equal(t, "dev", app.restSrv.Authenticator.Providers()[4].Name(), "dev auth provider") // send ping resp, err := http.Get(fmt.Sprintf("http://localhost:%d/api/v1/ping", port)) @@ -101,7 +101,7 @@ func TestServerApp_AnonMode(t *testing.T) { go func() { _ = app.run(ctx) }() waitForHTTPServerStart(port) - assert.Equal(t, 5+1, len(app.restSrv.Authenticator.Providers()), "extra auth provider for anon") + require.Equal(t, 5+1, len(app.restSrv.Authenticator.Providers()), "extra auth provider for anon") assert.Equal(t, "anonymous", app.restSrv.Authenticator.Providers()[5].Name(), "anon auth provider") // send ping @@ -382,7 +382,7 @@ func TestServerAuthHooks(t *testing.T) { t.Log(tk) // add comment - client := http.Client{Timeout: 2 * time.Second} + client := http.Client{Timeout: 10 * time.Second} req, err := http.NewRequest("POST", fmt.Sprintf("http://localhost:%d/api/v1/comment", port), strings.NewReader(`{"text": "test 123", "locator":{"url": "https://radio-t.com/p/2018/12/29/podcast-630/", "site": "remark"}}`)) require.NoError(t, err) diff --git a/backend/app/main_test.go b/backend/app/main_test.go index 65da9ea1..92f5e98d 100644 --- a/backend/app/main_test.go +++ b/backend/app/main_test.go @@ -41,6 +41,12 @@ func Test_Main(t *testing.T) { close(finished) }() + // defer cleanup because require check below can fail + defer func() { + close(done) + <-finished + }() + waitForHTTPServerStart(port) resp, err := http.Get(fmt.Sprintf("http://localhost:%d/api/v1/ping", port)) require.NoError(t, err) @@ -49,9 +55,6 @@ func Test_Main(t *testing.T) { body, err := ioutil.ReadAll(resp.Body) assert.NoError(t, err) assert.Equal(t, "pong", string(body)) - - close(done) - <-finished } func TestGetDump(t *testing.T) { diff --git a/backend/app/migrator/backup_test.go b/backend/app/migrator/backup_test.go index f86b6c91..94103fb1 100644 --- a/backend/app/migrator/backup_test.go +++ b/backend/app/migrator/backup_test.go @@ -10,6 +10,7 @@ import ( "time" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) func TestBackup_RemoveOldBackupFiles(t *testing.T) { @@ -31,7 +32,7 @@ func TestBackup_RemoveOldBackupFiles(t *testing.T) { bk.removeOldBackupFiles() ff, err := ioutil.ReadDir(loc) assert.NoError(t, err) - assert.Equal(t, 4, len(ff), "should keep 4 files - 3 kept for sit1, and one for site2") + require.Equal(t, 4, len(ff), "should keep 4 files - 3 kept for sit1, and one for site2") assert.Equal(t, "backup-site1-20171208.gz", ff[0].Name()) assert.Equal(t, "backup-site1-20171209.gz", ff[1].Name()) assert.Equal(t, "backup-site1-20171210.gz", ff[2].Name()) diff --git a/backend/app/migrator/disqus_test.go b/backend/app/migrator/disqus_test.go index d5fd2adc..a742ce47 100644 --- a/backend/app/migrator/disqus_test.go +++ b/backend/app/migrator/disqus_test.go @@ -29,7 +29,7 @@ func TestDisqus_Import(t *testing.T) { last, err := dataStore.Last("test", 10, time.Time{}, adminUser) assert.NoError(t, err) - assert.Equal(t, 4, len(last), "4 comments imported") + require.Equal(t, 4, len(last), "4 comments imported") c := last[len(last)-1] // last reverses, get first one assert.True(t, strings.HasPrefix(c.Text, "

The quick brown fox")) @@ -57,7 +57,7 @@ func TestDisqus_Convert(t *testing.T) { for comment := range ch { res = append(res, comment) } - assert.Equal(t, 4, len(res), "4 comments total, 1 spam excluded, 1 bad excluded") + require.Equal(t, 4, len(res), "4 comments total, 1 spam excluded, 1 bad excluded") exp0 := store.Comment{ ID: "299619020", diff --git a/backend/app/migrator/native_test.go b/backend/app/migrator/native_test.go index 81eb93d7..a22abb01 100644 --- a/backend/app/migrator/native_test.go +++ b/backend/app/migrator/native_test.go @@ -47,7 +47,7 @@ func TestNative_Export(t *testing.T) { require.NoError(t, dec.Decode(&m), "decode meta") - assert.Equal(t, 2, len(m.Users)) + require.Equal(t, 2, len(m.Users)) assert.Equal(t, "user1", m.Users[0].ID) assert.Equal(t, false, m.Users[0].Blocked.Status) assert.Equal(t, true, m.Users[0].Verified) @@ -55,7 +55,7 @@ func TestNative_Export(t *testing.T) { assert.Equal(t, true, m.Users[1].Blocked.Status) assert.Equal(t, false, m.Users[1].Verified) - assert.Equal(t, 1, len(m.Posts)) + require.Equal(t, 1, len(m.Posts)) assert.Equal(t, "https://radio-t.com", m.Posts[0].URL) assert.Equal(t, true, m.Posts[0].ReadOnly) @@ -84,7 +84,7 @@ func TestNative_Import(t *testing.T) { comments, err := b.Last("radio-t", 10, time.Time{}, store.User{}) assert.NoError(t, err) - assert.Equal(t, 2, len(comments)) + require.Equal(t, 2, len(comments)) assert.Equal(t, "f863bd79-fec6-4a75-b308-61fe5dd02aa1", comments[0].ID) assert.Equal(t, "1234", comments[0].ParentID) assert.Equal(t, false, b.IsReadOnly(comments[0].Locator)) @@ -122,7 +122,7 @@ func TestNative_ImportWithMapper(t *testing.T) { comments, err := b.Last("radio-t", 10, time.Time{}, store.User{}) assert.NoError(t, err) - assert.Equal(t, 2, len(comments)) + require.Equal(t, 2, len(comments)) assert.Equal(t, "f863bd79-fec6-4a75-b308-61fe5dd02aa1", comments[0].ID) assert.Equal(t, "1234", comments[0].ParentID) assert.Equal(t, false, b.IsReadOnly(comments[0].Locator)) diff --git a/backend/app/migrator/wordpress_test.go b/backend/app/migrator/wordpress_test.go index 2aab1572..bbf2e4cf 100644 --- a/backend/app/migrator/wordpress_test.go +++ b/backend/app/migrator/wordpress_test.go @@ -8,6 +8,7 @@ import ( bolt "github.com/coreos/bbolt" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" "github.com/umputun/remark/backend/app/store" "github.com/umputun/remark/backend/app/store/admin" @@ -30,7 +31,7 @@ func TestWordPress_Import(t *testing.T) { last, err := dataStore.Last(siteID, 10, time.Time{}, adminUser) assert.NoError(t, err) - assert.Equal(t, 3, len(last), "3 comments imported") + require.Equal(t, 3, len(last), "3 comments imported") c := last[0] assert.Equal(t, "14", c.ID) @@ -44,7 +45,7 @@ func TestWordPress_Import(t *testing.T) { posts, err := dataStore.List(siteID, 0, 0) assert.NoError(t, err) - assert.Equal(t, 1, len(posts)) + require.Equal(t, 1, len(posts)) p := posts[0] assert.Equal(t, "https://realmenweardress.es/2010/07/do-you-rp/", p.URL) @@ -62,7 +63,7 @@ func TestWordPress_Convert(t *testing.T) { for c := range ch { comments = append(comments, c) } - assert.Equal(t, 3, len(comments), "3 comments exported, 1 excluded") + require.Equal(t, 3, len(comments), "3 comments exported, 1 excluded") exp1 := store.Comment{ ID: "13", @@ -89,7 +90,7 @@ func TestWP_Convert_MD(t *testing.T) { for c := range ch { comments = append(comments, c) } - assert.Equal(t, 3, len(comments), "3 comments exported") + require.Equal(t, 3, len(comments), "3 comments exported") assert.Equal(t, "

Row1
\nRow2

\n\n

Row4

\n", comments[0].Text) diff --git a/backend/app/notify/notify_test.go b/backend/app/notify/notify_test.go index 0f53afbc..4cb73443 100644 --- a/backend/app/notify/notify_test.go +++ b/backend/app/notify/notify_test.go @@ -9,6 +9,7 @@ import ( "time" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" "github.com/umputun/remark/backend/app/store" ) @@ -36,8 +37,8 @@ func TestService_WithDestinations(t *testing.T) { time.Sleep(time.Millisecond * 110) s.Close() - assert.Equal(t, 3, len(d1.Get()), "got all comments to d1") - assert.Equal(t, 3, len(d2.Get()), "got all comments to d2") + require.Equal(t, 3, len(d1.Get()), "got all comments to d1") + require.Equal(t, 3, len(d2.Get()), "got all comments to d2") assert.Equal(t, "100", d1.Get()[0].Comment.ID) assert.Equal(t, "101", d1.Get()[1].Comment.ID) @@ -98,7 +99,7 @@ func TestService_WithParent(t *testing.T) { s.Close() destRes := dest.Get() - assert.Equal(t, 2, len(destRes), "two comment notified") + require.Equal(t, 2, len(destRes), "two comment notified") assert.Equal(t, "p1", destRes[0].Comment.ParentID) assert.Equal(t, "p1", destRes[0].parent.ID) assert.Equal(t, "p11", destRes[1].Comment.ParentID) diff --git a/backend/app/rest/api/admin_test.go b/backend/app/rest/api/admin_test.go index 69e4af1a..531b8279 100644 --- a/backend/app/rest/api/admin_test.go +++ b/backend/app/rest/api/admin_test.go @@ -183,7 +183,7 @@ func TestAdmin_DeleteUser(t *testing.T) { cmntWithInfo := commentsWithInfo{} err = json.Unmarshal([]byte(res), &cmntWithInfo) assert.NoError(t, err) - assert.Equal(t, 3, len(cmntWithInfo.Comments), "should have 3 comment") + require.Equal(t, 3, len(cmntWithInfo.Comments), "should have 3 comment") // id1 comment untouched assert.Equal(t, id1, cmntWithInfo.Comments[0].ID) @@ -317,7 +317,7 @@ func TestAdmin_Block(t *testing.T) { comments := commentsWithInfo{} err = json.Unmarshal([]byte(res), &comments) assert.NoError(t, err) - assert.Equal(t, 2, len(comments.Comments), "should have 2 comments") + require.Equal(t, 2, len(comments.Comments), "should have 2 comments") assert.Equal(t, "", comments.Comments[0].Text, "permanent block clear comment") assert.True(t, comments.Comments[0].Deleted, "permanent block set deleted comment's status") @@ -339,7 +339,7 @@ func TestAdmin_Block(t *testing.T) { comments = commentsWithInfo{} err = json.Unmarshal([]byte(res), &comments) assert.NoError(t, err) - assert.Equal(t, 4, len(comments.Comments), "should have 4 comments") + require.Equal(t, 4, len(comments.Comments), "should have 4 comments") assert.Equal(t, "test test #1", comments.Comments[2].Text, "comment not removed and not cleared") assert.False(t, comments.Comments[2].Deleted, "not deleted") @@ -351,7 +351,7 @@ func TestAdmin_Block(t *testing.T) { comments = commentsWithInfo{} err = json.Unmarshal([]byte(res), &comments) assert.NoError(t, err) - assert.Equal(t, 4, len(comments.Comments), "should have 4 comments") + require.Equal(t, 4, len(comments.Comments), "should have 4 comments") assert.Equal(t, "test test #1", comments.Comments[2].Text, "restored") assert.False(t, comments.Comments[2].Deleted) @@ -398,7 +398,7 @@ func TestAdmin_BlockedList(t *testing.T) { users := []store.BlockedUser{} err = json.NewDecoder(res.Body).Decode(&users) assert.NoError(t, err) - assert.Equal(t, 2, len(users), "two users blocked") + require.Equal(t, 2, len(users), "two users blocked") assert.Equal(t, "user1", users[0].ID) assert.Equal(t, "user1 name", users[0].Name) assert.Equal(t, "user2", users[1].ID) @@ -578,7 +578,7 @@ func TestAdmin_Verify(t *testing.T) { comments := commentsWithInfo{} err = json.Unmarshal([]byte(res), &comments) assert.NoError(t, err) - assert.Equal(t, 2, len(comments.Comments), "should have 2 comments") + require.Equal(t, 2, len(comments.Comments), "should have 2 comments") assert.Equal(t, "test test #1", comments.Comments[0].Text) assert.True(t, comments.Comments[0].User.Verified) @@ -596,7 +596,7 @@ func TestAdmin_Verify(t *testing.T) { comments = commentsWithInfo{} err = json.Unmarshal([]byte(res), &comments) assert.NoError(t, err) - assert.Equal(t, 2, len(comments.Comments), "should have 2 comments") + require.Equal(t, 2, len(comments.Comments), "should have 2 comments") assert.Equal(t, "test test #1", comments.Comments[0].Text) assert.False(t, comments.Comments[0].User.Verified) } diff --git a/backend/app/rest/api/rest_private_test.go b/backend/app/rest/api/rest_private_test.go index 5647c88b..9c940665 100644 --- a/backend/app/rest/api/rest_private_test.go +++ b/backend/app/rest/api/rest_private_test.go @@ -892,7 +892,7 @@ func TestRest_CreateWithPictures(t *testing.T) { Location: "/tmp/remark42/images", MaxSize: 2000, } - imageService.TTL = 50 * time.Millisecond + imageService.TTL = 100 * time.Millisecond svc.privRest.imageService = imageService svc.ImageService = imageService @@ -948,7 +948,7 @@ func TestRest_CreateWithPictures(t *testing.T) { assert.Error(t, err, "picture %d not moved from staging yet", i) } - time.Sleep(500 * time.Millisecond) + time.Sleep(1500 * time.Millisecond) for i := range ids { _, err = os.Stat("/tmp/remark42/images/" + ids[i]) diff --git a/backend/app/rest/api/rest_public_test.go b/backend/app/rest/api/rest_public_test.go index 6232ba38..85e2e5a7 100644 --- a/backend/app/rest/api/rest_public_test.go +++ b/backend/app/rest/api/rest_public_test.go @@ -99,7 +99,7 @@ func TestRest_Find(t *testing.T) { comments = commentsWithInfo{} err = json.Unmarshal([]byte(res), &comments) assert.NoError(t, err) - assert.Equal(t, 2, len(comments.Comments), "should have 2 comments") + require.Equal(t, 2, len(comments.Comments), "should have 2 comments") assert.Equal(t, id1, comments.Comments[0].ID) assert.Equal(t, id2, comments.Comments[1].ID) assert.Equal(t, "

test test #1

\n", comments.Comments[0].Text) @@ -114,7 +114,7 @@ func TestRest_Find(t *testing.T) { assert.Equal(t, 200, code) err = json.Unmarshal([]byte(res), &comments) assert.NoError(t, err) - assert.Equal(t, 2, len(comments.Comments), "should have 2 comments") + require.Equal(t, 2, len(comments.Comments), "should have 2 comments") assert.Equal(t, id1, comments.Comments[1].ID) assert.Equal(t, id2, comments.Comments[0].ID) @@ -124,7 +124,7 @@ func TestRest_Find(t *testing.T) { assert.Equal(t, 200, code) err = json.Unmarshal([]byte(res), &tree) assert.NoError(t, err) - assert.Equal(t, 1, len(tree.Nodes)) + require.Equal(t, 1, len(tree.Nodes)) assert.Equal(t, 1, len(tree.Nodes[0].Replies)) assert.Equal(t, 2, tree.Info.Count) assert.Equal(t, "https://radio-t.com/blah1", tree.Info.URL) @@ -230,7 +230,7 @@ func TestRest_FindUserView(t *testing.T) { comments = commentsWithInfo{} err = json.Unmarshal([]byte(res), &comments) assert.NoError(t, err) - assert.Equal(t, 2, len(comments.Comments), "should have 2 comments") + require.Equal(t, 2, len(comments.Comments), "should have 2 comments") assert.Equal(t, id1, comments.Comments[0].ID) assert.Equal(t, id2, comments.Comments[1].ID) assert.Equal(t, "dev", comments.Comments[0].User.ID) @@ -265,7 +265,7 @@ func TestRest_Last(t *testing.T) { comments := []store.Comment{} err := json.Unmarshal([]byte(res), &comments) assert.NoError(t, err) - assert.Equal(t, 2, len(comments), "should have 2 comments") + require.Equal(t, 2, len(comments), "should have 2 comments") assert.Equal(t, id1, comments[1].ID) assert.Equal(t, id2, comments[0].ID) @@ -274,7 +274,7 @@ func TestRest_Last(t *testing.T) { comments = []store.Comment{} err = json.Unmarshal([]byte(res), &comments) assert.NoError(t, err) - assert.Equal(t, 2, len(comments), "should have 2 comments") + require.Equal(t, 2, len(comments), "should have 2 comments") assert.Equal(t, id1, comments[1].ID) assert.Equal(t, id2, comments[0].ID) @@ -283,7 +283,7 @@ func TestRest_Last(t *testing.T) { comments = []store.Comment{} err = json.Unmarshal([]byte(res), &comments) assert.NoError(t, err) - assert.Equal(t, 1, len(comments), "should have 1 comments") + require.Equal(t, 1, len(comments), "should have 1 comments") assert.Equal(t, id2, comments[0].ID) res, code = get(t, ts.URL+"/api/v1/last/5?site=remark42") @@ -343,7 +343,7 @@ func TestRest_FindUserComments(t *testing.T) { err = json.Unmarshal([]byte(res), &resp) assert.NoError(t, err) - assert.Equal(t, 3, len(resp.Comments), "should have 3 comments") + require.Equal(t, 3, len(resp.Comments), "should have 3 comments") assert.Equal(t, 4, resp.Count, "should have 3 count") // user comment sorted with -time diff --git a/backend/app/rest/api/rest_test.go b/backend/app/rest/api/rest_test.go index 1f9450ad..42710de5 100644 --- a/backend/app/rest/api/rest_test.go +++ b/backend/app/rest/api/rest_test.go @@ -81,20 +81,20 @@ func TestRest_GetStarted(t *testing.T) { func TestRest_Shutdown(t *testing.T) { srv := Rest{Authenticator: &auth.Service{}, ImageProxy: &proxy.Image{}} - finished := make(chan bool) + done := make(chan bool) // without waiting for channel close at the end goroutine will stay alive after test finish // which would create data race with next test go func() { time.Sleep(200 * time.Millisecond) srv.Shutdown() - close(finished) + close(done) }() st := time.Now() srv.Run(0) assert.True(t, time.Since(st).Seconds() < 1, "should take about 100ms") - <-finished + <-done } func TestRest_filterComments(t *testing.T) { diff --git a/backend/app/store/engine/bolt_test.go b/backend/app/store/engine/bolt_test.go index 39d4d735..941b469a 100644 --- a/backend/app/store/engine/bolt_test.go +++ b/backend/app/store/engine/bolt_test.go @@ -25,7 +25,7 @@ func TestBoltDB_CreateAndFind(t *testing.T) { req := FindRequest{Locator: store.Locator{URL: "https://radio-t.com", SiteID: "radio-t"}, Sort: "time"} res, err := b.Find(req) assert.NoError(t, err) - assert.Equal(t, 2, len(res)) + require.Equal(t, 2, len(res)) assert.Equal(t, `some text, link`, res[0].Text) assert.Equal(t, "user1", res[0].User.ID) t.Log(res[0].ID) @@ -98,7 +98,7 @@ func TestBoltDB_Update(t *testing.T) { req := FindRequest{Locator: store.Locator{URL: "https://radio-t.com", SiteID: "radio-t"}, Sort: "time"} res, err := b.Find(req) assert.NoError(t, err) - assert.Equal(t, 2, len(res), "2 records initially") + require.Equal(t, 2, len(res), "2 records initially") comment := res[0] comment.Text = "abc 123" @@ -129,13 +129,13 @@ func TestBoltDB_FindLast(t *testing.T) { req := FindRequest{Locator: store.Locator{SiteID: "radio-t"}, Sort: "-time"} res, err := b.Find(req) assert.NoError(t, err) - assert.Equal(t, 2, len(res)) + require.Equal(t, 2, len(res)) assert.Equal(t, "some text2", res[0].Text) req.Limit = 1 res, err = b.Find(req) assert.NoError(t, err) - assert.Equal(t, 1, len(res)) + require.Equal(t, 1, len(res)) assert.Equal(t, "some text2", res[0].Text) req.Locator.SiteID = "bad" @@ -151,13 +151,13 @@ func TestBoltDB_FindLastSince(t *testing.T) { req := FindRequest{Locator: store.Locator{SiteID: "radio-t"}, Sort: "-time", Since: ts} res, err := b.Find(req) assert.NoError(t, err) - assert.Equal(t, 2, len(res)) + require.Equal(t, 2, len(res)) assert.Equal(t, "some text2", res[0].Text) req.Since = time.Date(2017, 12, 20, 15, 18, 22, 0, time.Local) res, err = b.Find(req) assert.NoError(t, err) - assert.Equal(t, 1, len(res)) + require.Equal(t, 1, len(res)) assert.Equal(t, "some text2", res[0].Text) req.Since = time.Date(2017, 12, 20, 16, 18, 22, 0, time.Local) @@ -174,13 +174,13 @@ func TestBoltDB_FindInPostSince(t *testing.T) { req := FindRequest{Locator: store.Locator{URL: "https://radio-t.com", SiteID: "radio-t"}, Sort: "-time", Since: ts} res, err := b.Find(req) assert.NoError(t, err) - assert.Equal(t, 2, len(res)) + require.Equal(t, 2, len(res)) assert.Equal(t, "some text2", res[0].Text) req.Since = time.Date(2017, 12, 20, 15, 18, 22, 0, time.Local) res, err = b.Find(req) assert.NoError(t, err) - assert.Equal(t, 1, len(res)) + require.Equal(t, 1, len(res)) assert.Equal(t, "some text2", res[0].Text) req.Since = time.Date(2017, 12, 20, 16, 18, 22, 0, time.Local) @@ -196,19 +196,19 @@ func TestBoltDB_FindForUser(t *testing.T) { req := FindRequest{Locator: store.Locator{SiteID: "radio-t"}, Sort: "-time", UserID: "user1", Limit: 5} res, err := b.Find(req) assert.NoError(t, err) - assert.Equal(t, 2, len(res)) + require.Equal(t, 2, len(res)) assert.Equal(t, "some text2", res[0].Text, "sorted by -time") req = FindRequest{Locator: store.Locator{SiteID: "radio-t"}, Sort: "-time", UserID: "user1", Limit: 1} res, err = b.Find(req) assert.NoError(t, err) - assert.Equal(t, 1, len(res), "allow 1 comment") + require.Equal(t, 1, len(res), "allow 1 comment") assert.Equal(t, "some text2", res[0].Text, "sorted by -time") req = FindRequest{Locator: store.Locator{SiteID: "radio-t"}, Sort: "-time", UserID: "user1", Limit: 1, Skip: 1} res, err = b.Find(req) assert.NoError(t, err) - assert.Equal(t, 1, len(res), "allow 1 comment") + require.Equal(t, 1, len(res), "allow 1 comment") assert.Equal(t, `some text, link`, res[0].Text, "second comment") req = FindRequest{Locator: store.Locator{SiteID: "bad"}, Sort: "-time", UserID: "user1", Limit: 1, Skip: 1} @@ -255,7 +255,7 @@ func TestBoltDB_FindForUserPagination(t *testing.T) { req.Limit = 5 res, err = b.Find(req) assert.NoError(t, err) - assert.Equal(t, 5, len(res)) + require.Equal(t, 5, len(res)) assert.Equal(t, "id-199", res[0].ID) assert.Equal(t, "id-195", res[4].ID) @@ -263,7 +263,7 @@ func TestBoltDB_FindForUserPagination(t *testing.T) { req.Skip, req.Limit = 10, 3 res, err = b.Find(req) assert.NoError(t, err) - assert.Equal(t, 3, len(res)) + require.Equal(t, 3, len(res)) assert.Equal(t, "id-189", res[0].ID) assert.Equal(t, "id-187", res[2].ID) @@ -271,7 +271,7 @@ func TestBoltDB_FindForUserPagination(t *testing.T) { req.Skip, req.Limit = 195, 10 res, err = b.Find(req) assert.NoError(t, err) - assert.Equal(t, 5, len(res)) + require.Equal(t, 5, len(res)) assert.Equal(t, "id-4", res[0].ID) assert.Equal(t, "id-0", res[4].ID) @@ -602,7 +602,7 @@ func TestBolt_FlagListBlocked(t *testing.T) { assert.NoError(t, err) blockedList := toBlocked(vv) - assert.Equal(t, 2, len(blockedList)) + require.Equal(t, 2, len(blockedList)) assert.Equal(t, "user1", blockedList[0].ID) assert.Equal(t, "user2", blockedList[1].ID) t.Logf("%+v", blockedList) @@ -612,7 +612,7 @@ func TestBolt_FlagListBlocked(t *testing.T) { vv, err = b.ListFlags(FlagRequest{Flag: Blocked, Locator: store.Locator{SiteID: "radio-t"}}) assert.NoError(t, err) blockedList = toBlocked(vv) - assert.Equal(t, 1, len(blockedList)) + require.Equal(t, 1, len(blockedList)) assert.Equal(t, "user1", blockedList[0].ID) _, err = b.ListFlags(FlagRequest{Flag: Blocked, Locator: store.Locator{SiteID: "bad"}}) @@ -689,7 +689,7 @@ func TestBolt_DeleteComment(t *testing.T) { res, err = b.Find(reqReq) assert.NoError(t, err) - assert.Equal(t, 2, len(res)) + require.Equal(t, 2, len(res)) assert.Equal(t, "", res[0].Text) assert.True(t, res[0].Deleted, "marked deleted") assert.Equal(t, store.User{Name: "user name", ID: "user1", Picture: "", Admin: false, Blocked: false, IP: ""}, res[0].User) @@ -727,7 +727,7 @@ func TestBolt_DeleteHard(t *testing.T) { reqReq := FindRequest{Locator: store.Locator{URL: "https://radio-t.com", SiteID: "radio-t"}, Sort: "time"} res, err := b.Find(reqReq) assert.NoError(t, err) - assert.Equal(t, 2, len(res), "initially 2 comments") + require.Equal(t, 2, len(res), "initially 2 comments") delReq := DeleteRequest{Locator: store.Locator{URL: "https://radio-t.com", SiteID: "radio-t"}, CommentID: res[0].ID, DeleteMode: store.HardDelete} @@ -736,7 +736,7 @@ func TestBolt_DeleteHard(t *testing.T) { res, err = b.Find(reqReq) assert.NoError(t, err) - assert.Equal(t, 2, len(res)) + require.Equal(t, 2, len(res)) assert.Equal(t, "", res[0].Text) assert.True(t, res[0].Deleted, "marked deleted") assert.Equal(t, store.User{Name: "deleted", ID: "deleted", Picture: "", Admin: false, Blocked: false, IP: ""}, res[0].User) @@ -812,7 +812,7 @@ func TestBoltAdmin_DeleteUserHard(t *testing.T) { comments, err := b.Find(FindRequest{Locator: store.Locator{SiteID: "radio-t", URL: "https://radio-t.com"}, Sort: "time"}) assert.NoError(t, err) - assert.Equal(t, 2, len(comments), "2 comments with deleted info") + require.Equal(t, 2, len(comments), "2 comments with deleted info") assert.Equal(t, store.User{Name: "deleted", ID: "deleted", Picture: "", Admin: false, Blocked: false, IP: ""}, comments[0].User) assert.Equal(t, store.User{Name: "deleted", ID: "deleted", Picture: "", Admin: false, Blocked: false, IP: ""}, comments[1].User) @@ -841,7 +841,7 @@ func TestBoltAdmin_DeleteUserSoft(t *testing.T) { comments, err := b.Find(FindRequest{Locator: store.Locator{SiteID: "radio-t", URL: "https://radio-t.com"}, Sort: "time"}) assert.NoError(t, err) - assert.Equal(t, 2, len(comments), "2 comments with deleted info") + require.Equal(t, 2, len(comments), "2 comments with deleted info") assert.Equal(t, store.User{Name: "user name", ID: "user1", Picture: "", Admin: false, Blocked: false, IP: ""}, comments[0].User) assert.Equal(t, store.User{Name: "user name", ID: "user1", Picture: "", Admin: false, Blocked: false, IP: ""}, comments[1].User) @@ -851,7 +851,7 @@ func TestBoltAdmin_DeleteUserSoft(t *testing.T) { comments, err = b.Find(FindRequest{Locator: store.Locator{SiteID: "radio-t"}, UserID: "user1", Limit: 5}) assert.NoError(t, err, "no comments for user user1 in store") - assert.Equal(t, 2, len(comments), "2 comments with deleted info") + require.Equal(t, 2, len(comments), "2 comments with deleted info") assert.True(t, comments[0].Deleted) assert.True(t, comments[1].Deleted) assert.Equal(t, "", comments[0].Text) diff --git a/backend/app/store/image/image_test.go b/backend/app/store/image/image_test.go index 3373b6ab..215768a4 100644 --- a/backend/app/store/image/image_test.go +++ b/backend/app/store/image/image_test.go @@ -20,7 +20,7 @@ func TestService_ExtractPictures(t *testing.T) { xyz

123

` ids, err := svc.ExtractPictures(html) require.NoError(t, err) - assert.Equal(t, 2, len(ids), "two images") + require.Equal(t, 2, len(ids), "two images") assert.Equal(t, "user1/pic1.png", ids[0]) assert.Equal(t, "user2/pic3.png", ids[1]) } @@ -31,7 +31,7 @@ func TestService_ExtractPictures2(t *testing.T) { "

\n\n

Пакеты в го это средство организации кода, они могут быть связанны друг с другом в рамках одной библиотеки (модуля). Например одна из моих вот так выглядит на libraries.io:

\n\n

\"bjtr0-201906-08110846-i324c.png\"/

\n\n

По форме все верно, это все packages, но по сути это все одна библиотека организованная таким образом. При ее импорте, например посредством go mod, она выглядит как один модуль, т.е. github.com/go-pkgz/auth v0.5.2.

\n" ids, err := svc.ExtractPictures(html) require.NoError(t, err) - assert.Equal(t, 1, len(ids), "one image in") + require.Equal(t, 1, len(ids), "one image in") assert.Equal(t, "github_ef0f706a79cc24b17bbbb374cd234a691d034128/bjttt8ahajfmrhsula10.png", ids[0]) } diff --git a/backend/app/store/service/service_test.go b/backend/app/store/service/service_test.go index 7c2a1c7a..22dbc303 100644 --- a/backend/app/store/service/service_test.go +++ b/backend/app/store/service/service_test.go @@ -198,7 +198,7 @@ func TestService_Vote(t *testing.T) { res, err := b.Last("radio-t", 0, time.Time{}, store.User{}) t.Logf("%+v", res[0]) assert.NoError(t, err) - assert.Equal(t, 3, len(res)) + require.Equal(t, 3, len(res)) assert.Equal(t, 0, res[0].Score) assert.Equal(t, 0, res[0].Vote) assert.Equal(t, map[string]bool(nil), res[0].Votes, "no votes initially") @@ -254,7 +254,7 @@ func TestService_Vote(t *testing.T) { res, err = b.Last("radio-t", 0, time.Time{}, store.User{ID: "user1"}) assert.NoError(t, err) t.Logf("%+v", res[0]) - assert.Equal(t, 3, len(res)) + require.Equal(t, 3, len(res)) assert.Equal(t, 1, res[0].Score) assert.Equal(t, 1, res[0].Vote) assert.Equal(t, 0.0, res[0].Controversy) @@ -263,7 +263,7 @@ func TestService_Vote(t *testing.T) { res, err = b.Last("radio-t", 0, time.Time{}, store.User{ID: "user2"}) assert.NoError(t, err) t.Logf("%+v", res[0]) - assert.Equal(t, 3, len(res)) + require.Equal(t, 3, len(res)) assert.Equal(t, 1, res[0].Score) assert.Equal(t, 0, res[0].Vote) assert.Equal(t, 0.0, res[0].Controversy) @@ -279,7 +279,7 @@ func TestService_Vote(t *testing.T) { assert.NoError(t, err, "vote reset") res, err = b.Last("radio-t", 0, time.Time{}, store.User{}) assert.NoError(t, err) - assert.Equal(t, 3, len(res)) + require.Equal(t, 3, len(res)) assert.Equal(t, 0, res[0].Score) assert.Equal(t, 0, res[0].Vote) assert.Equal(t, map[string]bool(nil), res[0].Votes, "vote reset ok") @@ -334,7 +334,7 @@ func TestService_VoteAggressive(t *testing.T) { res, err := b.Last("radio-t", 0, time.Time{}, store.User{}) require.NoError(t, err) t.Logf("%+v", res[0]) - assert.Equal(t, 3, len(res)) + require.Equal(t, 3, len(res)) assert.Equal(t, 0, res[0].Score) assert.Equal(t, map[string]bool(nil), res[0].Votes, "no votes initially") @@ -358,7 +358,7 @@ func TestService_VoteAggressive(t *testing.T) { require.NoError(t, err) t.Logf("%+v", res[0]) - assert.Equal(t, 3, len(res)) + require.Equal(t, 3, len(res)) assert.Equal(t, 2, res[0].Score, "add single +1") assert.Equal(t, 1, res[0].Vote, "user1 voted +1") assert.Equal(t, 0, len(res[0].Votes), "votes hidden") @@ -377,7 +377,7 @@ func TestService_VoteAggressive(t *testing.T) { wg.Wait() res, err = b.Last("radio-t", 0, time.Time{}, store.User{}) require.NoError(t, err) - assert.Equal(t, 3, len(res)) + require.Equal(t, 3, len(res)) t.Logf("%+v %d", res[0], res[0].Score) assert.True(t, res[0].Score >= 0 && res[0].Score <= 2, "unexpected score %d", res[0].Score) } @@ -500,7 +500,7 @@ func TestService_VoteSameIPWithDuration(t *testing.T) { MaxVotes: -1} defer b.Close() b.RestrictSameIPVotes.Enabled = true - b.RestrictSameIPVotes.Duration = 50 * time.Millisecond + b.RestrictSameIPVotes.Duration = 500 * time.Millisecond c, err := b.Vote(VoteReq{Locator: store.Locator{URL: "https://radio-t.com", SiteID: "radio-t"}, CommentID: "id-2", UserID: "user2", UserIP: "123", Val: true}) @@ -517,7 +517,7 @@ func TestService_VoteSameIPWithDuration(t *testing.T) { assert.NoError(t, err) assert.Equal(t, 2, c.Score, "have 2 score") - time.Sleep(51 * time.Millisecond) + time.Sleep(501 * time.Millisecond) c, err = b.Vote(VoteReq{Locator: store.Locator{URL: "https://radio-t.com", SiteID: "radio-t"}, CommentID: "id-2", UserID: "user3", UserIP: "123", Val: true}) @@ -557,7 +557,7 @@ func TestService_Pin(t *testing.T) { res, err := b.Last("radio-t", 0, time.Time{}, store.User{}) t.Logf("%+v", res[0]) assert.NoError(t, err) - assert.Equal(t, 2, len(res)) + require.Equal(t, 2, len(res)) assert.Equal(t, false, res[0].Pin) err = b.SetPin(store.Locator{URL: "https://radio-t.com", SiteID: "radio-t"}, res[0].ID, true) @@ -582,7 +582,7 @@ func TestService_EditComment(t *testing.T) { res, err := b.Last("radio-t", 0, time.Time{}, store.User{}) t.Logf("%+v", res[0]) assert.NoError(t, err) - assert.Equal(t, 2, len(res)) + require.Equal(t, 2, len(res)) assert.Nil(t, res[0].Edit) comment, err := b.EditComment(store.Locator{URL: "https://radio-t.com", SiteID: "radio-t"}, res[0].ID, @@ -610,7 +610,7 @@ func TestService_DeleteComment(t *testing.T) { res, err := b.Last("radio-t", 0, time.Time{}, store.User{}) t.Logf("%+v", res[0]) assert.NoError(t, err) - assert.Equal(t, 2, len(res)) + require.Equal(t, 2, len(res)) assert.Nil(t, res[0].Edit) _, err = b.EditComment(store.Locator{URL: "https://radio-t.com", SiteID: "radio-t"}, res[0].ID, EditRequest{Delete: true}) @@ -631,7 +631,7 @@ func TestService_EditCommentDurationFailed(t *testing.T) { res, err := b.Last("radio-t", 0, time.Time{}, store.User{}) t.Logf("%+v", res[0]) assert.NoError(t, err) - assert.Equal(t, 2, len(res)) + require.Equal(t, 2, len(res)) assert.Nil(t, res[0].Edit) time.Sleep(time.Second) @@ -649,7 +649,7 @@ func TestService_EditCommentReplyFailed(t *testing.T) { res, err := b.Last("radio-t", 0, time.Time{}, store.User{}) t.Logf("%+v", res[1]) assert.NoError(t, err) - assert.Equal(t, 2, len(res)) + require.Equal(t, 2, len(res)) assert.Nil(t, res[1].Edit) reply := store.Comment{ @@ -754,7 +754,7 @@ func TestService_GetMetas(t *testing.T) { um, pm, err = b.Metas("radio-t") require.NoError(t, err) - assert.Equal(t, 3, len(um)) + require.Equal(t, 3, len(um)) assert.Equal(t, "user1", um[0].ID) assert.Equal(t, true, um[0].Verified) assert.Equal(t, engine.UserDetailEntry{Email: ""}, um[0].Details) @@ -765,7 +765,7 @@ func TestService_GetMetas(t *testing.T) { assert.Equal(t, "user3", um[2].ID) assert.Equal(t, "test@example.org", um[2].Details.Email) - assert.Equal(t, 1, len(pm)) + require.Equal(t, 1, len(pm)) assert.Equal(t, "https://radio-t.com", pm[0].URL) assert.Equal(t, true, pm[0].ReadOnly) } @@ -937,12 +937,12 @@ func TestService_UserReplies(t *testing.T) { cc, u, err := b.UserReplies("radio-t", "u1", 10, time.Hour) assert.NoError(t, err) - assert.Equal(t, 3, len(cc), "3 replies to u1") + require.Equal(t, 3, len(cc), "3 replies to u1") assert.Equal(t, "developer one u1", u) - cc, u, err = b.UserReplies("radio-t", "u1", 10, time.Millisecond*150) + cc, u, err = b.UserReplies("radio-t", "u1", 10, time.Millisecond*199) assert.NoError(t, err) - assert.Equal(t, 1, len(cc), "1 reply to u1 in last 150ms") + require.Equal(t, 1, len(cc), "1 reply to u1 in last 200ms") assert.Equal(t, "developer one u1", u) cc, u, err = b.UserReplies("radio-t", "u2", 10, time.Hour) @@ -985,7 +985,7 @@ func TestService_Find(t *testing.T) { // make sure Controversy altered res, err = b.Find(store.Locator{URL: "https://radio-t.com", SiteID: "radio-t"}, "-controversy", store.User{}) require.NoError(t, err) - assert.Equal(t, 3, len(res)) + require.Equal(t, 3, len(res)) assert.Equal(t, "123456", res[0].ID) assert.InDelta(t, 1.73, res[0].Controversy, 0.01) assert.Equal(t, "id-1", res[1].ID) @@ -1000,13 +1000,13 @@ func TestService_FindSince(t *testing.T) { res, err := b.FindSince(store.Locator{URL: "https://radio-t.com", SiteID: "radio-t"}, "time", store.User{}, time.Time{}) require.NoError(t, err) - assert.Equal(t, 2, len(res)) + require.Equal(t, 2, len(res)) assert.Equal(t, "id-1", res[0].ID) res, err = b.FindSince(store.Locator{URL: "https://radio-t.com", SiteID: "radio-t"}, "time", store.User{}, time.Date(2017, 12, 20, 15, 18, 22, 0, time.Local)) require.NoError(t, err) - assert.Equal(t, 1, len(res)) + require.Equal(t, 1, len(res)) assert.Equal(t, "id-2", res[0].ID) } @@ -1041,7 +1041,7 @@ func TestService_Delete(t *testing.T) { defer b.Close() res, err := b.Last("radio-t", 0, time.Time{}, store.User{}) - assert.Equal(t, 2, len(res)) + require.Equal(t, 2, len(res)) assert.NoError(t, err) err = b.Delete(store.Locator{URL: "https://radio-t.com", SiteID: "radio-t"}, res[0].ID, store.SoftDelete) @@ -1079,7 +1079,7 @@ func TestService_DeleteUser(t *testing.T) { assert.NoError(t, err) res, err = b.Last("radio-t", 0, time.Time{}, store.User{}) - assert.Equal(t, 1, len(res), "only one comment left for user2") + require.Equal(t, 1, len(res), "only one comment left for user2") assert.NoError(t, err) assert.Equal(t, "user2", res[0].User.ID) } @@ -1104,7 +1104,7 @@ func TestService_List(t *testing.T) { res, err := b.List("radio-t", 0, 0) assert.NoError(t, err) - assert.Equal(t, 2, len(res), "2 posts") + require.Equal(t, 2, len(res), "2 posts") assert.Equal(t, "https://radio-t.com/2", res[0].URL) assert.Equal(t, 1, res[0].Count) assert.Equal(t, time.Date(2018, 12, 20, 15, 18, 22, 0, time.Local), res[0].FirstTS) @@ -1168,7 +1168,7 @@ func TestService_UserComments(t *testing.T) { cc, err := b.User("radio-t", "user1", 0, 0, store.User{}) assert.NoError(t, err) - assert.Equal(t, 2, len(cc), "two recs for user1") + require.Equal(t, 2, len(cc), "two recs for user1") assert.Equal(t, "id-2", cc[0].ID, "reverse sort") assert.Equal(t, "id-1", cc[1].ID, "reverse sort") }