2018-01-29 20:37:26 +01:00
|
|
|
package reflector
|
2017-08-15 22:02:18 +02:00
|
|
|
|
|
|
|
import (
|
2018-04-03 18:14:04 +02:00
|
|
|
"crypto/rand"
|
2017-08-15 22:02:18 +02:00
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"strconv"
|
|
|
|
"testing"
|
2018-02-07 21:21:20 +01:00
|
|
|
|
|
|
|
"github.com/lbryio/reflector.go/store"
|
2018-05-30 03:38:55 +02:00
|
|
|
log "github.com/sirupsen/logrus"
|
2017-08-15 22:02:18 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
var address = "localhost:" + strconv.Itoa(DefaultPort)
|
|
|
|
|
|
|
|
func TestMain(m *testing.M) {
|
|
|
|
dir, err := ioutil.TempDir("", "reflector_client_test")
|
|
|
|
if err != nil {
|
2018-05-30 03:38:55 +02:00
|
|
|
log.Panic("could not create temp directory - ", err)
|
2017-08-15 22:02:18 +02:00
|
|
|
}
|
2018-05-30 03:38:55 +02:00
|
|
|
defer func(directory string) {
|
2018-06-15 04:30:37 +02:00
|
|
|
err := os.RemoveAll(dir)
|
|
|
|
if err != nil {
|
2018-05-30 03:38:55 +02:00
|
|
|
log.Panic("error removing files and directory - ", err)
|
|
|
|
}
|
|
|
|
}(dir)
|
2017-08-15 22:02:18 +02:00
|
|
|
|
2018-02-07 21:21:20 +01:00
|
|
|
ms := store.MemoryBlobStore{}
|
|
|
|
s := NewServer(&ms)
|
2018-05-30 03:38:55 +02:00
|
|
|
go func() {
|
2018-06-15 04:30:37 +02:00
|
|
|
err := s.Start(address)
|
|
|
|
if err != nil {
|
2018-05-30 03:38:55 +02:00
|
|
|
log.Panic("error starting up reflector server - ", err)
|
|
|
|
}
|
|
|
|
}()
|
2017-08-15 22:02:18 +02:00
|
|
|
|
|
|
|
os.Exit(m.Run())
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNotConnected(t *testing.T) {
|
|
|
|
c := Client{}
|
|
|
|
err := c.SendBlob([]byte{})
|
|
|
|
if err == nil {
|
|
|
|
t.Error("client should error if it is not connected")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSmallBlob(t *testing.T) {
|
|
|
|
c := Client{}
|
|
|
|
err := c.Connect(address)
|
|
|
|
if err != nil {
|
2018-05-30 03:38:55 +02:00
|
|
|
t.Error("error connecting client to server - ", err)
|
2017-08-15 22:02:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
err = c.SendBlob([]byte{})
|
|
|
|
if err == nil {
|
|
|
|
t.Error("client should error if blob is empty")
|
|
|
|
}
|
|
|
|
|
|
|
|
blob := make([]byte, 1000)
|
|
|
|
_, err = rand.Read(blob)
|
|
|
|
if err != nil {
|
|
|
|
t.Error("failed to make random blob")
|
|
|
|
}
|
|
|
|
|
|
|
|
err = c.SendBlob([]byte{})
|
|
|
|
if err == nil {
|
|
|
|
t.Error("client should error if blob is the wrong size")
|
|
|
|
}
|
|
|
|
}
|