diff --git a/test/nodejs/announce_test.go b/test/nodejs/announce_test.go index b43a3e0..94c7597 100644 --- a/test/nodejs/announce_test.go +++ b/test/nodejs/announce_test.go @@ -18,45 +18,21 @@ func TestJSClient(t *testing.T) { // defer leakcheck.Check(t) // r := require.New(t) - // ts := newRandomSession(t) - ts := newSession(t, nil) + ts := newRandomSession(t) + // ts := newSession(t, nil) - ts.startGoServer() - s := ts.gobot + srv := ts.startGoServer() - alice := ts.startJSBot(` - sbot.on('rpc:connect', rpc => { - var ret = rpc.tunnel.announce() - t.comment('announced') - console.warn(ret) - pull( - rpc.tunnel.endpoints(), - pull.drain(el => { - console.warn("from roomsrv:",el) - }) - ) + alice := ts.startJSBot("./testscripts/simple_client.js", + srv.Network.GetListenAddr(), + srv.Whoami(), + ) - setTimeout(() => { - ret = rpc.tunnel.leave() - t.comment('left') - console.warn(ret) - }, 2500) - - setTimeout(() => { - t.comment('shutting down') - exit() - }, 5000) - }) - run()`, ``) - - s.Allow(alice, true) + srv.Allow(alice, true) time.Sleep(5 * time.Second) ts.wait() - - // TODO: check wantManager for this connection is stopped when the jsbot exited - } func TestJSServer(t *testing.T) { @@ -66,17 +42,17 @@ func TestJSServer(t *testing.T) { os.RemoveAll("testrun") - // ts := newRandomSession(t) - ts := newSession(t, nil) + ts := newRandomSession(t) + // ts := newSession(t, nil) - ts.startGoServer() - client := ts.gobot + client := ts.startGoServer() // alice is the server now alice, port := ts.startJSBotAsServer("alice", "./testscripts/server.js") client.Allow(*alice, true) + // connect to alice wrappedAddr := netwrap.WrapAddr(&net.TCPAddr{ IP: net.ParseIP("127.0.0.1"), Port: port, diff --git a/test/nodejs/sbot_client.js b/test/nodejs/sbot_client.js index 7e0fff9..7e32808 100644 --- a/test/nodejs/sbot_client.js +++ b/test/nodejs/sbot_client.js @@ -1,9 +1,6 @@ const Path = require('path') -const { readFileSync } = require('fs') const { loadOrCreateSync } = require('ssb-keys') -const pull = require('pull-stream') // used in eval scripts const tape = require('tape') -const parallel = require('run-parallel') // used in eval scripts const theStack = require('secret-stack') const ssbCaps = require('ssb-caps') @@ -14,7 +11,6 @@ if (testSHSappKey !== false) { testAppkey = testSHSappKey } - const createSbot = theStack({caps: {shs: testAppkey } }) .use(require('ssb-db')) .use(require('ssb-gossip')) @@ -23,11 +19,12 @@ const createSbot = theStack({caps: {shs: testAppkey } }) .use(require('ssb-room/tunnel/client')) const testName = process.env.TEST_NAME -const testBob = process.env.TEST_BOB -const testAddr = process.env.TEST_GOADDR -const scriptBefore = readFileSync(process.env.TEST_BEFORE).toString() -const scriptAfter = readFileSync(process.env.TEST_AFTER).toString() +// the other peer we are talking to +const testPeerAddr = process.env.TEST_PEERADDR +const testPeerRef = process.env.TEST_PEERREF + +const testSession = require(process.env['TEST_SESSIONSCRIPT']) function bufFromEnv(evname) { const has = process.env[evname] @@ -39,20 +36,19 @@ function bufFromEnv(evname) { tape.createStream().pipe(process.stderr) tape(testName, function (t) { - let timeoutLength = 15000 var tapeTimeout = null - function run() { // needs to be called by the before block when it's done + function ready() { // needs to be called by the before block when it's done t.timeoutAfter(timeoutLength) // doesn't exit the process tapeTimeout = setTimeout(() => { t.comment('test timeout') process.exit(1) }, timeoutLength*1.25) - const to = `net:${testAddr}~shs:${testBob.substr(1).replace('.ed25519', '')}` + const to = `net:${testPeerAddr}~shs:${testPeerRef.substr(1).replace('.ed25519', '')}` t.comment('dialing:' + to) sbot.connect(to, (err) => { t.error(err, 'connected') - eval(scriptAfter) + testSession.after(sbot, exit) }) } @@ -82,5 +78,5 @@ tape(testName, function (t) { t.comment('sbot spawned, running before') console.log(alice.id) // tell go process who's incoming - eval(scriptBefore) + testSession.before(sbot, ready) }) diff --git a/test/nodejs/sbot_serv.js b/test/nodejs/sbot_serv.js index 9f07ed8..0038d09 100644 --- a/test/nodejs/sbot_serv.js +++ b/test/nodejs/sbot_serv.js @@ -1,6 +1,5 @@ const Path = require('path') const tape = require('tape') -const { readFileSync } = require('fs') const { loadOrCreateSync } = require('ssb-keys') const theStack = require('secret-stack') const ssbCaps = require('ssb-caps') @@ -12,8 +11,8 @@ if (testSHSappKey !== false) { testAppkey = testSHSappKey } -stackOpts = {appKey: require('ssb-caps').shs} -// stackOpts = {caps: {shs: testAppkey } } +// stackOpts = {appKey: require('ssb-caps').shs} +stackOpts = {caps: {shs: testAppkey } } const createSbot = theStack(stackOpts) .use(require('ssb-db')) .use(require('ssb-master')) @@ -23,10 +22,7 @@ const createSbot = theStack(stackOpts) const testName = process.env['TEST_NAME'] const testPort = process.env['TEST_PORT'] - -const testSession = require(process.env['TEST_BEFORE']) -// const scriptBefore = readFileSync( -// const scriptAfter = readFileSync(process.env['TEST_AFTER']).toString() +const testSession = require(process.env['TEST_SESSIONSCRIPT']) tape.createStream().pipe(process.stderr); tape(testName, function (t) { diff --git a/test/nodejs/setup_test.go b/test/nodejs/setup_test.go index 099e383..0644e24 100644 --- a/test/nodejs/setup_test.go +++ b/test/nodejs/setup_test.go @@ -8,8 +8,8 @@ import ( "context" "crypto/rand" "encoding/base64" + "errors" "fmt" - "io/ioutil" mrand "math/rand" "net" "os" @@ -38,17 +38,6 @@ func init() { } } -func writeFile(t *testing.T, data string) string { - r := require.New(t) - f, err := ioutil.TempFile("testrun/"+t.Name(), "*.js") - r.NoError(err) - _, err = fmt.Fprintf(f, "%s", data) - r.NoError(err) - err = f.Close() - r.NoError(err) - return f.Name() -} - type testSession struct { t *testing.T @@ -58,9 +47,6 @@ type testSession struct { keySHS []byte - // TODO: multiple by name?! - gobot *roomsrv.Server - done errgroup.Group ctx context.Context @@ -121,15 +107,15 @@ func (ts *testSession) startGoServer(opts ...roomsrv.Option) *roomsrv.Server { srv, err := roomsrv.New(opts...) r.NoError(err, "failed to init tees a server") - ts.t.Logf("go server: %s", srv.Whoami()) + ts.t.Logf("go server: %s", srv.Whoami().Ref()) ts.t.Cleanup(func() { - srv.Close() + ts.t.Log("bot close:", srv.Close()) }) - ts.gobot = srv + ts.done.Go(func() error { err := srv.Network.Serve(ts.ctx) - if err != nil { - err = fmt.Errorf("node serve exited: %w", err) + if err != nil && !errors.Is(err, context.Canceled) { + err = fmt.Errorf("go server exited: %w", err) ts.t.Log(err) return err } @@ -141,12 +127,12 @@ func (ts *testSession) startGoServer(opts ...roomsrv.Option) *roomsrv.Server { var jsBotCnt = 0 -func (ts *testSession) startJSBot(jsbefore, jsafter string) refs.FeedRef { - return ts.startJSBotWithName("", jsbefore, jsafter) +func (ts *testSession) startJSBot(testScript string, peerAddr net.Addr, peerRef refs.FeedRef) refs.FeedRef { + return ts.startJSBotWithName("", testScript, peerAddr, peerRef) } // returns the jsbots pubkey -func (ts *testSession) startJSBotWithName(name, jsbefore, jsafter string) refs.FeedRef { +func (ts *testSession) startJSBotWithName(name, testScript string, peerAddr net.Addr, peerRef refs.FeedRef) refs.FeedRef { ts.t.Log("starting client", name) r := require.New(ts.t) cmd := exec.CommandContext(ts.ctx, "node", "./sbot_client.js") @@ -160,14 +146,14 @@ func (ts *testSession) startJSBotWithName(name, jsbefore, jsafter string) refs.F } jsBotCnt++ - // TODO: pass goref's via function? - // TODO or nickname solution + // ts.gobot.Network.GetListenAddr() + // ts.gobot.Whoami() + env := []string{ "TEST_NAME=" + name, - "TEST_BOB=" + ts.gobot.Whoami().Ref(), - "TEST_GOADDR=" + netwrap.GetAddr(ts.gobot.Network.GetListenAddr(), "tcp").String(), - "TEST_BEFORE=" + writeFile(ts.t, jsbefore), - "TEST_AFTER=" + writeFile(ts.t, jsafter), + "TEST_PEERADDR=" + netwrap.GetAddr(peerAddr, "tcp").String(), + "TEST_PEERREF=" + peerRef.Ref(), + "TEST_SESSIONSCRIPT=" + testScript, } if ts.keySHS != nil { @@ -213,13 +199,10 @@ func (ts *testSession) startJSBotAsServer(name, testScriptFileName string) (*ref var port = 1024 + mrand.Intn(23000) - // TODO: pass goref's via function? - // TODO or nickname solution env := []string{ "TEST_NAME=" + filepath.Join(ts.t.Name(), "jsbot-"+name), - "TEST_BOB=" + ts.gobot.Whoami().Ref(), fmt.Sprintf("TEST_PORT=%d", port), - "TEST_BEFORE=" + testScriptFileName, + "TEST_SESSIONSCRIPT=" + testScriptFileName, } if ts.keySHS != nil { env = append(env, "TEST_APPKEY="+base64.StdEncoding.EncodeToString(ts.keySHS)) @@ -249,8 +232,6 @@ func (ts *testSession) startJSBotAsServer(name, testScriptFileName string) (*ref } func (ts *testSession) wait() { - ts.gobot.Shutdown() - assert.NoError(ts.t, ts.gobot.Close()) ts.cancel() assert.NoError(ts.t, ts.done.Wait()) diff --git a/test/nodejs/testscripts/simple_client.js b/test/nodejs/testscripts/simple_client.js new file mode 100644 index 0000000..66297fc --- /dev/null +++ b/test/nodejs/testscripts/simple_client.js @@ -0,0 +1,30 @@ +const pull = require('pull-stream') + +module.exports = { + before: (sbot, ready) => { + sbot.on('rpc:connect', rpc => { + var ret = rpc.tunnel.announce() + console.warn('announced') + console.warn(ret) + pull( + rpc.tunnel.endpoints(), + pull.drain(el => { + console.warn("from roomsrv:",el) + }) + ) + + setTimeout(() => { + ret = rpc.tunnel.leave() + console.warn('left') + console.warn(ret) + }, 2500) + }) + ready() + }, + + after: (sbot, exit) => { + console.warn('after connect...') + + setTimeout(exit, 5000) + } +} \ No newline at end of file diff --git a/test/nodejs/testscripts/template.js b/test/nodejs/testscripts/template.js new file mode 100644 index 0000000..9f7e747 --- /dev/null +++ b/test/nodejs/testscripts/template.js @@ -0,0 +1,14 @@ +const pull = require('pull-stream') + +module.exports = { + before: (sbot, ready) => { + console.warn('before connect...') + setTimeout(ready, 1000) + }, + + after: (sbot, exit) => { + console.warn('after connect...') + + setTimeout(exit, 5000) + } +} \ No newline at end of file