Skip to content

Commit

Permalink
file streamer test
Browse files Browse the repository at this point in the history
  • Loading branch information
kmulvey committed Sep 9, 2022
1 parent ecdb8ce commit c33e72f
Showing 1 changed file with 65 additions and 0 deletions.
65 changes: 65 additions & 0 deletions internal/app/imagedup/files_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package imagedup

import (
"context"
"testing"

"github.com/kmulvey/path"
"github.com/stretchr/testify/assert"
)

var expectedPairs = map[string]struct{}{
"testimages/iceland-small.jpgtestimages/iceland.jpg": {},
"testimages/iceland-small.jpgtestimages/trees.jpg": {},
"testimages/iceland.jpgtestimages/trees.jpg": {},
}

func TestStreamFiles(t *testing.T) {
t.Parallel()

var id, err = NewImageDup(context.Background(), "TestStreamFiles", "cacheFile.json", "outputFile.log", 2, 10)
assert.NoError(t, err)

var done = make(chan struct{})
go func() {
for img := range id.images {
delete(expectedPairs, img.One+img.Two)
}
assert.Equal(t, 0, len(expectedPairs))

close(done)
}()

files, err := path.ListFiles("./testimages")
assert.NoError(t, err)
files = path.OnlyFiles(files)
var fileNames = path.OnlyNames(files)

id.streamFiles(fileNames)

<-done
}

func TestStreamFilesCancel(t *testing.T) {
t.Parallel()

var ctx, cancel = context.WithCancel(context.Background())
var id, err = NewImageDup(ctx, "TestStreamFilesCancel", "cacheFile.json", "outputFile.log", 2, 10)
assert.NoError(t, err)

var done = make(chan struct{})
go func() {
var numImages int
for range id.images {
numImages++
}
assert.True(t, numImages < 100) // kind of arbitrary, it basically just needs to be small

close(done)
}()

go id.streamFiles(make([]string, 100))
cancel()

<-done
}

0 comments on commit c33e72f

Please sign in to comment.