-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsnapshot_test.go
67 lines (57 loc) · 1.5 KB
/
snapshot_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
package main
import (
"bytes"
"context"
"fmt"
"github.com/mattn/go-mjpeg"
"github.com/minio/minio-go"
"github.com/stretchr/testify/assert"
"image"
"image/jpeg"
"io"
"net/http/httptest"
"testing"
"time"
)
type fakeMinio struct {
}
func (fakeMinio) PutObject(bucketName, objectName string, reader io.Reader, objectSize int64, opts minio.PutObjectOptions) (n int64, err error) {
return 1, nil
}
func TestGetSnapshotUrl(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
stream := mjpeg.NewStream()
server := httptest.NewServer(stream)
defer server.Close()
client := &SnapshotClient{
StorageClient: fakeMinio{},
targetCamUrl: server.URL,
domain: "test.zone",
bucket: "test",
cacheChan: make(chan image.Image),
}
go client.imagePreCache(ctx)
//go func() {client.cacheChan <- image.NewGray(image.Rect(0, 0, 100, 100))}()
go func() {
for {
uniformImage := image.NewGray(image.Rect(0, 0, 1000, 1000))
buf := bytes.Buffer{}
err := jpeg.Encode(&buf, uniformImage, &jpeg.Options{Quality: 100})
assert.NoError(t, err)
err = stream.Update(buf.Bytes())
assert.NoError(t, err)
select {
case <-ctx.Done():
err := stream.Close()
assert.NoError(t, err)
return
case <-time.After(time.Millisecond * 100):
}
}
}()
url, err := client.GetSnapshotUrl()
assert.NoError(t, err)
cancel()
assert.Regexp(t, fmt.Sprintf("https://%v.%v/%v/snapshots/.*/snapshot.jpg", client.bucket, client.domain, client.bucket), url)
}