diff options
author | 2022-08-31 17:31:21 +0200 | |
---|---|---|
committer | 2022-08-31 17:31:21 +0200 | |
commit | 0245c606d77c8b99833ccc2c0923a298fb482236 (patch) | |
tree | 16311e89656894f09cfaeb8b0f21b5ac9e4de502 /testrig | |
parent | [feature] add rate limit middleware (#741) (diff) | |
download | gotosocial-0245c606d77c8b99833ccc2c0923a298fb482236.tar.xz |
[chore] Test fixes (#788)
* use 'test' value for testrig storage backend
* update test dependency
* add WaitFor func in testrig
* use WaitFor function instead of time.Sleep
* tidy up tests
* make SentMessages a sync.map
* go fmt
Diffstat (limited to 'testrig')
-rw-r--r-- | testrig/config.go | 15 | ||||
-rw-r--r-- | testrig/transportcontroller.go | 15 | ||||
-rw-r--r-- | testrig/util.go | 25 |
3 files changed, 43 insertions, 12 deletions
diff --git a/testrig/config.go b/testrig/config.go index 59f7e6df3..9de23dfc3 100644 --- a/testrig/config.go +++ b/testrig/config.go @@ -19,9 +19,6 @@ package testrig import ( - "os" - "path" - "github.com/coreos/go-oidc/v3/oidc" "github.com/superseriousbusiness/gotosocial/internal/config" ) @@ -29,12 +26,11 @@ import ( // InitTestConfig initializes viper configuration with test defaults. func InitTestConfig() { config.Config(func(cfg *config.Configuration) { - *cfg = TestDefaults + *cfg = testDefaults }) } -// TestDefaults returns a Values struct with values set that are suitable for local testing. -var TestDefaults = config.Configuration{ +var testDefaults = config.Configuration{ LogLevel: "trace", LogDbQueries: true, ApplicationName: "gotosocial", @@ -69,8 +65,11 @@ var TestDefaults = config.Configuration{ MediaDescriptionMaxChars: 500, MediaRemoteCacheDays: 30, - StorageBackend: "local", - StorageLocalBasePath: path.Join(os.TempDir(), "gotosocial"), + // the testrig only uses in-memory storage, so we can + // safely set this value to 'test' to avoid running storage + // migrations, and other silly things like that + StorageBackend: "test", + StorageLocalBasePath: "", StatusesMaxChars: 5000, StatusesCWMaxChars: 100, diff --git a/testrig/transportcontroller.go b/testrig/transportcontroller.go index 6edf2b52e..68f03398d 100644 --- a/testrig/transportcontroller.go +++ b/testrig/transportcontroller.go @@ -24,6 +24,7 @@ import ( "io" "net/http" "strings" + "sync" "github.com/superseriousbusiness/activity/pub" "github.com/superseriousbusiness/activity/streams" @@ -64,7 +65,7 @@ type MockHTTPClient struct { testRemoteServices map[string]vocab.ActivityStreamsService testRemoteAttachments map[string]RemoteAttachmentFile - SentMessages map[string][]byte + SentMessages sync.Map } // NewMockHTTPClient returns a client that conforms to the pub.HttpClient interface. @@ -90,8 +91,6 @@ func NewMockHTTPClient(do func(req *http.Request) (*http.Response, error), relat mockHTTPClient.testRemoteServices = NewTestFediServices() mockHTTPClient.testRemoteAttachments = NewTestFediAttachments(relativeMediaPath) - mockHTTPClient.SentMessages = make(map[string][]byte) - mockHTTPClient.do = func(req *http.Request) (*http.Response, error) { responseCode := http.StatusNotFound responseBytes := []byte(`{"error":"404 not found"}`) @@ -103,7 +102,15 @@ func NewMockHTTPClient(do func(req *http.Request) (*http.Response, error), relat if err != nil { panic(err) } - mockHTTPClient.SentMessages[req.URL.String()] = b + + if sI, loaded := mockHTTPClient.SentMessages.LoadOrStore(req.URL.String(), [][]byte{b}); loaded { + s, ok := sI.([][]byte) + if !ok { + panic("SentMessages entry wasn't [][]byte") + } + s = append(s, b) + mockHTTPClient.SentMessages.Store(req.URL.String(), s) + } responseCode = http.StatusOK responseBytes = []byte(`{"ok":"accepted"}`) diff --git a/testrig/util.go b/testrig/util.go index 3c0c5b52f..eb00ab362 100644 --- a/testrig/util.go +++ b/testrig/util.go @@ -87,3 +87,28 @@ func TimeMustParse(timeString string) time.Time { } return t } + +// WaitFor calls condition every 200ms, returning true +// when condition() returns true, or false after 5s. +// +// It's useful for when you're waiting for something to +// happen, but you don't know exactly how long it will take, +// and you want to fail if the thing doesn't happen within 5s. +func WaitFor(condition func() bool) bool { + tick := time.NewTicker(200 * time.Millisecond) + defer tick.Stop() + + timeout := time.NewTimer(5 * time.Second) + defer timeout.Stop() + + for { + select { + case <-tick.C: + if condition() { + return true + } + case <-timeout.C: + return false + } + } +} |