-
Notifications
You must be signed in to change notification settings - Fork 1
/
pool_test.go
93 lines (82 loc) · 2.05 KB
/
pool_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package gobergamot_test
import (
"bytes"
"context"
"testing"
"time"
"github.com/KSpaceer/gobergamot"
"github.com/KSpaceer/gobergamot/internal/wasm"
)
const (
helloWorldTranslation = "Здравствуйте Мир"
goodbyeWorldTranslation = "Прощание с миром"
)
func TestPool(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
t.Cleanup(cancel)
_, err := gobergamot.NewPool(ctx, gobergamot.PoolConfig{
Config: gobergamot.Config{
FilesBundle: gobergamot.FilesBundle{
Model: bytes.NewBuffer([]byte{}),
LexicalShortlist: bytes.NewBuffer([]byte{}),
Vocabulary: bytes.NewBuffer([]byte{}),
},
},
PoolSize: 3,
})
if err == nil {
t.Fatalf("NewPool should have failed")
}
stdout, stderr := bytes.NewBuffer(nil), bytes.NewBuffer(nil)
pool, err := gobergamot.NewPool(ctx, gobergamot.PoolConfig{
Config: gobergamot.Config{
CompileConfig: wasm.CompileConfig{
Stderr: stderr,
Stdout: stdout,
},
CacheSize: 100,
FilesBundle: testBundle(t),
},
PoolSize: 3,
})
if err != nil {
t.Fatalf("NewPool returned error %v", err)
}
t.Cleanup(func() {
if err := pool.Close(ctx); err != nil {
t.Fatalf("failed to close pool: %v", err)
}
})
requests := [...]gobergamot.TranslationRequest{
{Text: "Hello World"},
{Text: "Hello World"},
{Text: "Hello World"},
{Text: "Hello World"},
{Text: "Hello World"},
{Text: "Goodbye World"},
{Text: "Goodbye World"},
{Text: "Goodbye World"},
{Text: "Goodbye World"},
{Text: "Goodbye World"},
}
textChan := make(chan string, len(requests))
for _, req := range requests {
go func(req gobergamot.TranslationRequest) {
output, err := pool.Translate(ctx, req)
if err != nil {
panic(err)
}
textChan <- output
}(req)
}
for range requests {
select {
case <-ctx.Done():
t.Fatal("context timeout waiting for responses")
case output := <-textChan:
if output != helloWorldTranslation && output != goodbyeWorldTranslation {
t.Errorf("unexpected output %s", output)
}
}
}
}