-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
17 changed files
with
575 additions
and
128 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,18 @@ | ||
testonly: false | ||
with-expecter: true | ||
# inpackage: true | ||
# dir: mocks/{{ replaceAll .InterfaceDirRelative "internal" "internal_" }} | ||
# mockname: "{{.InterfaceName}}" | ||
# outpkg: "{{.PackageName}}" | ||
filename: "{{.InterfaceName}}.go" | ||
all: true | ||
packages: | ||
github.com/trezorg/lingualeo/pkg/translator: | ||
config: | ||
recursive: true | ||
with-expecter: true | ||
mockname: "Mock_{{.InterfaceName}}" | ||
# outpkg: mocks | ||
dir: ./{{ replaceAll .InterfaceDirRelative "internal" "internal_" }} | ||
filename: "mock_{{.InterfaceName}}.go" | ||
inpackage: true |
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
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
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
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
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
This file was deleted.
Oops, something went wrong.
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
This file was deleted.
Oops, something went wrong.
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,41 @@ | ||
package translator | ||
|
||
import ( | ||
"context" | ||
"sync" | ||
|
||
"github.com/trezorg/lingualeo/internal/files" | ||
"github.com/trezorg/lingualeo/pkg/channel" | ||
) | ||
|
||
// Downloader interface | ||
// | ||
//go:generate mockery | ||
type Downloader interface { | ||
Download(url string) (string, error) | ||
} | ||
|
||
// DownloadFiles download files from URLs channel | ||
func DownloadFiles(ctx context.Context, urls <-chan string, downloader Downloader) <-chan files.File { | ||
out := make(chan files.File) | ||
var wg sync.WaitGroup | ||
wg.Add(1) | ||
go func() { | ||
defer wg.Done() | ||
idx := 0 | ||
for url := range channel.OrDone(ctx, urls) { | ||
wg.Add(1) | ||
go func(idx int, url string) { | ||
defer wg.Done() | ||
filename, err := downloader.Download(url) | ||
out <- files.File{Error: err, Filename: filename, Index: idx} | ||
}(idx, url) | ||
idx++ | ||
} | ||
}() | ||
go func() { | ||
defer close(out) | ||
wg.Wait() | ||
}() | ||
return out | ||
} |
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,27 @@ | ||
package translator | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestDownloadWordFile(t *testing.T) { | ||
downloader := NewMock_Downloader(t) | ||
url := "http://test.com/file" | ||
testFile := "/tmp/test.file" | ||
|
||
downloader.EXPECT().Download(url).Return(testFile, nil).Once() | ||
|
||
inChan := make(chan string, 1) | ||
inChan <- url | ||
|
||
ctx := context.Background() | ||
|
||
close(inChan) | ||
|
||
out := DownloadFiles(ctx, inChan, downloader) | ||
fileName := (<-out).Filename | ||
assert.Equal(t, fileName, testFile) | ||
} |
Oops, something went wrong.