From 1e4d938689b6c15c2893af80ca31394dbff9bba4 Mon Sep 17 00:00:00 2001 From: Andrey Beletsky Date: Thu, 18 Jul 2019 00:45:04 +0700 Subject: [PATCH] Improve robustness of accounts API test --- api/accounts_test.go | 21 +++++++++++++++------ api/handlers_test.go | 2 +- internal/lbrynet/lbrynet.go | 2 +- 3 files changed, 17 insertions(+), 8 deletions(-) diff --git a/api/accounts_test.go b/api/accounts_test.go index 406dbfdb..3cd0cbba 100644 --- a/api/accounts_test.go +++ b/api/accounts_test.go @@ -24,6 +24,7 @@ import ( const dummyUserID = 751365 const dummyServerURL = "http://127.0.0.1:59999" const proxySuffix = "/api/proxy" +const testSetupWait = 200 * time.Millisecond func TestMain(m *testing.M) { // call flag.Parse() here if TestMain uses flags @@ -47,6 +48,11 @@ func TestMain(m *testing.M) { } func testFuncSetup() { + time.Sleep(testSetupWait) + lbrynet.RemoveAccount(dummyUserID) +} + +func testFuncTeardown() { lbrynet.RemoveAccount(dummyUserID) } @@ -69,6 +75,7 @@ func launchDummyAPIServerDelayed(response []byte, delayMsec time.Duration) *http func TestWithValidAuthToken(t *testing.T) { testFuncSetup() + defer testFuncTeardown() var ( q *jsonrpc.RPCRequest @@ -122,7 +129,8 @@ func TestWithValidAuthToken(t *testing.T) { } func TestWithValidAuthTokenConcurrent(t *testing.T) { - defer lbrynet.RemoveAccount(123) + testFuncSetup() + defer testFuncTeardown() var wg sync.WaitGroup @@ -130,7 +138,7 @@ func TestWithValidAuthTokenConcurrent(t *testing.T) { "success": true, "error": null, "data": { - "id": 123, + "id": 751365, "language": "en", "given_name": null, "family_name": null, @@ -165,12 +173,10 @@ func TestWithValidAuthTokenConcurrent(t *testing.T) { r.Header.Add("X-Lbry-Auth-Token", "d94ab9865f8416d107935d2ca644509c") rr := httptest.NewRecorder() - monitor.Logger.Debugf("request %v about to start", w) - http.HandlerFunc(Proxy).ServeHTTP(rr, r) - assert.Equal(t, http.StatusOK, rr.Code) + require.Equal(t, http.StatusOK, rr.Code) json.Unmarshal(rr.Body.Bytes(), &response) - assert.Nil(t, response.Error) + require.Nil(t, response.Error) monitor.Logger.Debugf("request %v done", w) wg.Done() }(w, &wg) @@ -180,6 +186,7 @@ func TestWithValidAuthTokenConcurrent(t *testing.T) { func TestWithWrongAuthToken(t *testing.T) { testFuncSetup() + defer testFuncTeardown() var ( q *jsonrpc.RPCRequest @@ -211,9 +218,11 @@ func TestWithWrongAuthToken(t *testing.T) { func TestWithoutToken(t *testing.T) { testFuncSetup() + defer testFuncTeardown() // Create a dummy account so we have a wallet beside the default one lbrynet.CreateAccount(999) + defer lbrynet.RemoveAccount(999) var ( q *jsonrpc.RPCRequest diff --git a/api/handlers_test.go b/api/handlers_test.go index e4fc825c..53115aa3 100644 --- a/api/handlers_test.go +++ b/api/handlers_test.go @@ -14,7 +14,7 @@ import ( "github.com/ybbus/jsonrpc" ) -func TestProxy_Options(t *testing.T) { +func TestProxyOptions(t *testing.T) { request, _ := http.NewRequest("OPTIONS", "/api/proxy", nil) rr := httptest.NewRecorder() http.HandlerFunc(Proxy).ServeHTTP(rr, request) diff --git a/internal/lbrynet/lbrynet.go b/internal/lbrynet/lbrynet.go index 9cf2ab85..67b6fc19 100644 --- a/internal/lbrynet/lbrynet.go +++ b/internal/lbrynet/lbrynet.go @@ -62,7 +62,7 @@ func RemoveAccount(UID int) (*ljsonrpc.AccountRemoveResponse, error) { if err != nil { return nil, err } - logger.Log().Infof("removing account %v", UID) + logger.LogF(monitor.F{"uid": UID, "account_id": acc.ID}).Warn("removing account from lbrynet") r, err := Client.AccountRemove(acc.ID) if err != nil { return nil, err