This repository has been archived by the owner on May 15, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add unit test to check that Put queue is being processed
- Loading branch information
Showing
1 changed file
with
83 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
package singularity_test | ||
|
||
import ( | ||
"context" | ||
"net/http" | ||
"net/http/httptest" | ||
"net/url" | ||
"os" | ||
"path/filepath" | ||
"testing" | ||
"time" | ||
|
||
singularityclient "github.com/data-preservation-programs/singularity/client/swagger/http" | ||
"github.com/filecoin-project/motion/integration/singularity" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestStorePut(t *testing.T) { | ||
testServer := httptest.NewServer(http.HandlerFunc(testHandler)) | ||
t.Cleanup(func() { | ||
testServer.Close() | ||
}) | ||
|
||
cfg := singularityclient.DefaultTransportConfig() | ||
u, _ := url.Parse(testServer.URL) | ||
cfg.Host = u.Host | ||
singularityAPI := singularityclient.NewHTTPClientWithConfig(nil, cfg) | ||
|
||
tmpDir := t.TempDir() | ||
s, err := singularity.NewStore( | ||
singularity.WithStoreDir(tmpDir), | ||
singularity.WithWalletKey("dummy"), | ||
singularity.WithSingularityClient(singularityAPI), | ||
) | ||
require.NoError(t, err) | ||
|
||
ctx := context.Background() | ||
s.Start(ctx) | ||
|
||
testFile := filepath.Join(tmpDir, "testdata.txt") | ||
f, err := os.Create(testFile) | ||
require.NoError(t, err) | ||
_, err = f.WriteString("Halló heimur!") | ||
require.NoError(t, err) | ||
require.NoError(t, f.Close()) | ||
f, err = os.Open(testFile) | ||
require.NoError(t, err) | ||
t.Cleanup(func() { | ||
f.Close() | ||
}) | ||
|
||
done := make(chan struct{}) | ||
go func() { | ||
defer close(done) | ||
// Putting test file more than size put queue. If this blocks, then | ||
// store.toPack channel is not being read. | ||
for i := 0; i < singularity.PutQueueSize+1; i++ { | ||
desc, err := s.Put(ctx, f) | ||
require.NoError(t, err) | ||
require.NotNil(t, desc) | ||
} | ||
}() | ||
|
||
timer := time.NewTimer(time.Second) | ||
select { | ||
case <-done: | ||
case <-timer.C: | ||
require.FailNow(t, "Put queue is not being read, check that store.runPreparationJobs is running") | ||
} | ||
timer.Stop() | ||
|
||
err = s.Shutdown(context.Background()) | ||
require.NoError(t, err) | ||
} | ||
|
||
func testHandler(w http.ResponseWriter, req *http.Request) { | ||
defer req.Body.Close() | ||
|
||
if req.URL.Path == "/api/preparation/MOTION_PREPARATION/schedules" && req.Method == http.MethodGet { | ||
http.Error(w, "", http.StatusNotFound) | ||
return | ||
} | ||
} |