go-gun/gun/tests/context_test.go

144 lines
3.8 KiB
Go
Raw Normal View History

2019-02-20 20:54:46 +00:00
package tests
import (
"bytes"
"context"
"log"
2019-02-22 06:46:19 +00:00
"os"
2019-02-20 20:54:46 +00:00
"os/exec"
"path/filepath"
"runtime"
"strconv"
"strings"
2019-02-20 20:54:46 +00:00
"testing"
"time"
2019-02-20 20:54:46 +00:00
2019-02-22 18:40:02 +00:00
"github.com/cretz/esgopeta/gun"
2019-02-20 20:54:46 +00:00
"github.com/stretchr/testify/require"
)
type testContext struct {
context.Context
*testing.T
2019-02-22 18:40:02 +00:00
Require *require.Assertions
GunJSPort int
2019-02-20 20:54:46 +00:00
}
const defaultTestTimeout = 1 * time.Minute
2019-02-20 20:54:46 +00:00
func newContext(t *testing.T) (*testContext, context.CancelFunc) {
return withTestContext(context.Background(), t)
}
2019-02-25 05:14:26 +00:00
func newContextWithGunJServer(t *testing.T) (*testContext, context.CancelFunc) {
ctx, cancelFn := newContext(t)
serverCancelFn := ctx.startGunJSServer()
return ctx, func() {
serverCancelFn()
cancelFn()
}
}
2019-02-22 18:40:02 +00:00
const defaultGunJSPort = 8080
const defaultRemoteGunServerURL = "https://gunjs.herokuapp.com/gun"
2019-02-22 18:40:02 +00:00
2019-02-20 20:54:46 +00:00
func withTestContext(ctx context.Context, t *testing.T) (*testContext, context.CancelFunc) {
ctx, cancelFn := context.WithTimeout(ctx, defaultTestTimeout)
2019-02-20 20:54:46 +00:00
return &testContext{
2019-02-22 18:40:02 +00:00
Context: ctx,
T: t,
Require: require.New(t),
GunJSPort: defaultGunJSPort,
2019-02-20 20:54:46 +00:00
}, cancelFn
}
func (t *testContext) debugf(format string, args ...interface{}) {
if testing.Verbose() {
log.Printf(format, args...)
}
}
func (t *testContext) runJS(script string) []byte {
cmd := exec.CommandContext(t, "node")
_, currFile, _, _ := runtime.Caller(0)
cmd.Dir = filepath.Dir(currFile)
cmd.Stdin = bytes.NewReader([]byte(script))
out, err := cmd.CombinedOutput()
out = removeGunJSWelcome(out)
t.Require.NoErrorf(err, "JS failure, output:\n%v", string(out))
return out
}
2019-02-22 18:40:02 +00:00
func (t *testContext) runJSWithGun(script string) []byte {
return t.runJSWithGunURL("http://127.0.0.1:"+strconv.Itoa(t.GunJSPort)+"/gun", script)
}
func (t *testContext) runJSWithGunURL(url string, script string) []byte {
2019-02-22 18:40:02 +00:00
return t.runJS(`
var Gun = require('gun')
const gun = Gun({
peers: ['` + url + `'],
2019-02-22 18:40:02 +00:00
radisk: false
})
` + script)
}
2019-02-20 20:54:46 +00:00
func (t *testContext) startJS(script string) (*bytes.Buffer, *exec.Cmd, context.CancelFunc) {
cmdCtx, cancelFn := context.WithCancel(t)
cmd := exec.CommandContext(cmdCtx, "node")
_, currFile, _, _ := runtime.Caller(0)
cmd.Dir = filepath.Dir(currFile)
cmd.Stdin = bytes.NewReader([]byte(script))
var buf bytes.Buffer
cmd.Stdout, cmd.Stderr = &buf, &buf
t.Require.NoError(cmd.Start())
return &buf, cmd, cancelFn
}
2019-02-25 05:14:26 +00:00
func (t *testContext) startGunJSServer() context.CancelFunc {
2019-02-22 18:40:02 +00:00
// If we're logging, use a proxy
port := t.GunJSPort
if testing.Verbose() {
t.startGunWebSocketProxyLogger(port, "ws://127.0.0.1:"+strconv.Itoa(port+1)+"/gun")
2019-02-22 18:40:02 +00:00
port++
}
2019-02-25 05:14:26 +00:00
// Remove entire data folder first just in case
2019-02-22 09:23:14 +00:00
t.Require.NoError(os.RemoveAll("radata-server"))
2019-02-25 05:14:26 +00:00
_, cmd, cancelFn := t.startJS(`
2019-02-20 20:54:46 +00:00
var Gun = require('gun')
const server = require('http').createServer().listen(` + strconv.Itoa(port) + `)
const gun = Gun({web: server, file: 'radata-server'})
`)
2019-02-25 05:14:26 +00:00
return func() {
cancelFn()
cmd.Wait()
// Remove the data folder at the end
os.RemoveAll("radata-server")
}
2019-02-20 20:54:46 +00:00
}
2019-02-22 18:40:02 +00:00
func (t *testContext) prepareRemoteGunServer(origURL string) (newURL string) {
// If we're verbose, use proxy, otherwise just use orig
if !testing.Verbose() {
return origURL
}
origURL = strings.Replace(origURL, "http://", "ws://", 1)
origURL = strings.Replace(origURL, "https://", "wss://", 1)
t.startGunWebSocketProxyLogger(t.GunJSPort, origURL)
return "http://127.0.0.1:" + strconv.Itoa(t.GunJSPort) + "/gun"
}
2019-02-22 18:40:02 +00:00
func (t *testContext) newGunConnectedToGunJS() *gun.Gun {
return t.newGunConnectedToGunServer("http://127.0.0.1:" + strconv.Itoa(t.GunJSPort) + "/gun")
}
func (t *testContext) newGunConnectedToGunServer(url string) *gun.Gun {
config := gun.Config{
PeerURLs: []string{url},
PeerErrorHandler: func(errPeer *gun.ErrPeer) { t.debugf("Got peer error: %v", errPeer) },
}
g, err := gun.New(t, config)
2019-02-22 18:40:02 +00:00
t.Require.NoError(err)
return g
}