Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 19 additions & 15 deletions web/files/recents_list_drives_test.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
package files

import (
"net/http"
"strings"
"sync/atomic"
"testing"
"time"

"github.com/cozy/cozy-stack/model/instance"
"github.com/cozy/cozy-stack/model/job"
"github.com/cozy/cozy-stack/model/recents"
"github.com/cozy/cozy-stack/model/sharing"
"github.com/cozy/cozy-stack/model/vfs"
"github.com/cozy/cozy-stack/pkg/config/config"
"github.com/cozy/cozy-stack/pkg/consts"
"github.com/cozy/cozy-stack/pkg/couchdb"
"github.com/cozy/cozy-stack/tests/testutils"
Expand Down Expand Up @@ -93,19 +91,25 @@ func (o *driveOwner) update(t *testing.T, id string, change func(*vfs.FileDoc))
require.NoError(t, couchdb.UpdateDoc(o.inst, doc))
}

// countCouchRequests returns the number of requests that fn sends to CouchDB.
func countCouchRequests(t *testing.T, fn func()) int {
// countRequests returns the number of requests that fn sends to the databases
// of the member and of the owner. The triggers on the files of the owner run
// jobs in the background. A thumbnail job reads the parent folder of its image
// to open it, so the count starts once these jobs are over. The other jobs
// only write in the databases of the jobs and read the global one.
func (o *driveOwner) countRequests(t *testing.T, fn func()) int {
t.Helper()
client := config.CouchClient()
original := client.Transport
defer func() { client.Transport = original }()
var count atomic.Int32
client.Transport = roundTripper(func(r *http.Request) (*http.Response, error) {
count.Add(1)
return original.RoundTrip(r)
testutils.WaitForOrFail(t, 10*time.Second, func() bool {
jobs, err := job.GetQueuedJobs(o.inst, "thumbnail")
return err == nil && len(jobs) == 0
})
fn()
return int(count.Load())
member := "/" + o.member.DBPrefix() + "/"
owner := "/" + o.inst.DBPrefix() + "/"
return countRequests(t, func(path string) bool {
if strings.Contains(path, "/io-cozy-jobs") {
return false
}
return strings.HasPrefix(path, member) || strings.HasPrefix(path, owner)
}, fn)
}

// getRecents returns the whole list, and its raw body.
Expand Down Expand Up @@ -216,7 +220,7 @@ func TestRecentsListCompletesTheDriveItems(t *testing.T) {

t.Run("the requests do not depend on the number of rows of an owner", func(t *testing.T) {
count := func(limit string) (requests int, ids []string) {
requests = countCouchRequests(t, func() {
requests = owner.countRequests(t, func() {
ids = idsOf(env.getPage(t, "page[limit]="+limit), "included")
})
return requests, ids
Expand Down
26 changes: 19 additions & 7 deletions web/files/recents_list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,17 +120,18 @@ type roundTripper func(*http.Request) (*http.Response, error)

func (f roundTripper) RoundTrip(r *http.Request) (*http.Response, error) { return f(r) }

// countFilesRequests returns the number of requests that fn sends to the
// databases of the files and of the recents. The other requests of a call
// (token, permissions, flags) do not depend on the page.
func countFilesRequests(t *testing.T, fn func()) int {
// countRequests returns the number of requests that fn sends to CouchDB on a
// path accepted by match. The triggers on the files run jobs in the background
// while fn runs, and those jobs send their own requests: match must keep them
// out of the count.
func countRequests(t *testing.T, match func(path string) bool, fn func()) int {
t.Helper()
client := config.CouchClient()
original := client.Transport
defer func() { client.Transport = original }()
var count atomic.Int32
client.Transport = roundTripper(func(r *http.Request) (*http.Response, error) {
if strings.Contains(r.URL.Path, "io-cozy-files") {
if match(r.URL.Path) {
count.Add(1)
}
return original.RoundTrip(r)
Expand All @@ -139,6 +140,17 @@ func countFilesRequests(t *testing.T, fn func()) int {
return int(count.Load())
}

// countFilesRequests returns the number of requests that fn sends to the
// databases of the files and of the recents of the instance. The other
// requests of a call (token, permissions, flags) do not depend on the page.
func (env *recentsEnv) countFilesRequests(t *testing.T, fn func()) int {
t.Helper()
files := "/" + env.inst.DBPrefix() + "/io-cozy-files"
return countRequests(t, func(path string) bool {
return strings.HasPrefix(path, files)
}, fn)
}

// idsOf returns the IDs of the "data" or of the "included" member of a page,
// in their order. A missing member has none.
func idsOf(page *httpexpect.Object, key string) []string {
Expand Down Expand Up @@ -183,8 +195,8 @@ func TestRecentsListPagination(t *testing.T) {
// The files are in the same folder: the path of a file asks for its
// parent folder, once per folder.
var one, all *httpexpect.Object
small := countFilesRequests(t, func() { one = env.getPage(t, "page[limit]=1") })
large := countFilesRequests(t, func() { all = env.getPage(t, "page[limit]=7") })
small := env.countFilesRequests(t, func() { one = env.getPage(t, "page[limit]=1") })
large := env.countFilesRequests(t, func() { all = env.getPage(t, "page[limit]=7") })
assert.Len(t, idsOf(one, "included"), 1)
assert.Len(t, idsOf(all, "included"), 7)
// One request for the marker of the seed, one for the rows, and one
Expand Down
Loading