2019-02-22 06:46:19 +00:00
|
|
|
package tests
|
|
|
|
|
2019-02-22 18:40:02 +00:00
|
|
|
import (
|
2019-02-25 04:23:15 +00:00
|
|
|
"strings"
|
2019-02-22 18:40:02 +00:00
|
|
|
"testing"
|
2019-02-22 19:51:50 +00:00
|
|
|
|
|
|
|
"github.com/cretz/esgopeta/gun"
|
2019-02-22 18:40:02 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestGunGetSimple(t *testing.T) {
|
2019-02-22 06:46:19 +00:00
|
|
|
// Run the server, put in one call, get in another, then check
|
2019-02-25 20:09:47 +00:00
|
|
|
ctx, cancelFn := newContext(t)
|
2019-02-22 06:46:19 +00:00
|
|
|
defer cancelFn()
|
2019-02-25 20:09:47 +00:00
|
|
|
serverCancelFn := ctx.startGunJSServer()
|
|
|
|
defer serverCancelFn()
|
2019-02-22 06:46:19 +00:00
|
|
|
randStr := randString(30)
|
2019-02-22 18:40:02 +00:00
|
|
|
// Write w/ JS
|
|
|
|
ctx.runJSWithGun(`
|
2019-02-26 21:59:44 +00:00
|
|
|
gun.get('esgopeta-test').get('TestGunGetSimple').get('some-field').put('` + randStr + `', ack => {
|
2019-02-22 06:46:19 +00:00
|
|
|
if (ack.err) {
|
|
|
|
console.error(ack.err)
|
|
|
|
process.exit(1)
|
|
|
|
}
|
|
|
|
process.exit(0)
|
|
|
|
})
|
|
|
|
`)
|
2019-02-22 18:40:02 +00:00
|
|
|
// Get
|
|
|
|
g := ctx.newGunConnectedToGunJS()
|
2019-02-22 21:40:55 +00:00
|
|
|
defer g.Close()
|
|
|
|
// Make sure we got back the same value
|
2019-02-26 21:59:44 +00:00
|
|
|
r := g.Scoped(ctx, "esgopeta-test", "TestGunGetSimple", "some-field").FetchOne(ctx)
|
2019-02-25 04:23:15 +00:00
|
|
|
ctx.Require.NoError(r.Err)
|
|
|
|
ctx.Require.Equal(gun.ValueString(randStr), r.Value.(gun.ValueString))
|
2019-02-25 20:09:47 +00:00
|
|
|
// Do it again with the JS server closed since it should fetch from memory
|
|
|
|
serverCancelFn()
|
2019-02-26 21:59:44 +00:00
|
|
|
ctx.debugf("Asking for field again")
|
|
|
|
r = g.Scoped(ctx, "esgopeta-test", "TestGunGetSimple", "some-field").FetchOne(ctx)
|
2019-02-25 20:09:47 +00:00
|
|
|
ctx.Require.NoError(r.Err)
|
|
|
|
ctx.Require.Equal(gun.ValueString(randStr), r.Value.(gun.ValueString))
|
2019-02-25 04:23:15 +00:00
|
|
|
}
|
2019-02-22 06:46:19 +00:00
|
|
|
|
2019-02-26 19:06:16 +00:00
|
|
|
func TestGunGetSimpleRemote(t *testing.T) {
|
|
|
|
// Do the above but w/ remote server
|
|
|
|
ctx, cancelFn := newContext(t)
|
|
|
|
defer cancelFn()
|
|
|
|
remoteURL := ctx.prepareRemoteGunServer(defaultRemoteGunServerURL)
|
2019-02-26 21:59:44 +00:00
|
|
|
randField, randVal := "field-"+randString(30), gun.ValueString(randString(30))
|
2019-02-26 19:06:16 +00:00
|
|
|
// Write w/ JS
|
|
|
|
ctx.debugf("Writing value")
|
|
|
|
ctx.runJSWithGunURL(remoteURL, `
|
2019-02-26 21:59:44 +00:00
|
|
|
gun.get('esgopeta-test').get('TestGunGetSimpleRemote').get('`+randField+`').put('`+string(randVal)+`', ack => {
|
2019-02-26 19:06:16 +00:00
|
|
|
if (ack.err) {
|
|
|
|
console.error(ack.err)
|
|
|
|
process.exit(1)
|
|
|
|
}
|
|
|
|
process.exit(0)
|
|
|
|
})
|
|
|
|
`)
|
|
|
|
// Get
|
|
|
|
ctx.debugf("Reading value")
|
|
|
|
g := ctx.newGunConnectedToGunServer(remoteURL)
|
|
|
|
defer g.Close()
|
|
|
|
// Make sure we got back the same value
|
2019-02-26 21:59:44 +00:00
|
|
|
r := g.Scoped(ctx, "esgopeta-test", "TestGunGetSimpleRemote", randField).FetchOne(ctx)
|
2019-02-26 19:06:16 +00:00
|
|
|
ctx.Require.NoError(r.Err)
|
|
|
|
ctx.Require.Equal(randVal, r.Value)
|
|
|
|
}
|
|
|
|
|
2019-02-25 04:23:15 +00:00
|
|
|
func TestGunPutSimple(t *testing.T) {
|
2019-02-25 05:14:26 +00:00
|
|
|
ctx, cancelFn := newContextWithGunJServer(t)
|
2019-02-25 04:23:15 +00:00
|
|
|
defer cancelFn()
|
|
|
|
randStr := randString(30)
|
|
|
|
// Put
|
|
|
|
g := ctx.newGunConnectedToGunJS()
|
|
|
|
defer g.Close()
|
|
|
|
// Just wait for two acks (one local, one remote)
|
2019-02-26 21:59:44 +00:00
|
|
|
ch := g.Scoped(ctx, "esgopeta-test", "TestGunPutSimple", "some-field").Put(ctx, gun.ValueString(randStr))
|
2019-02-25 04:23:15 +00:00
|
|
|
// TODO: test local is null peer and remote is non-null
|
|
|
|
r := <-ch
|
|
|
|
ctx.Require.NoError(r.Err)
|
|
|
|
r = <-ch
|
|
|
|
ctx.Require.NoError(r.Err)
|
|
|
|
// Get from JS
|
|
|
|
out := ctx.runJSWithGun(`
|
2019-02-26 21:59:44 +00:00
|
|
|
gun.get('esgopeta-test').get('TestGunPutSimple').get('some-field').once(data => {
|
2019-02-25 04:23:15 +00:00
|
|
|
console.log(data)
|
|
|
|
process.exit(0)
|
|
|
|
})
|
|
|
|
`)
|
|
|
|
ctx.Require.Equal(randStr, strings.TrimSpace(string(out)))
|
2019-02-22 06:46:19 +00:00
|
|
|
}
|
2019-02-25 20:09:47 +00:00
|
|
|
|
2019-02-26 19:06:16 +00:00
|
|
|
func TestGunPubSubSimpleRemote(t *testing.T) {
|
|
|
|
ctx, cancelFn := newContext(t)
|
|
|
|
defer cancelFn()
|
|
|
|
remoteURL := ctx.prepareRemoteGunServer(defaultRemoteGunServerURL)
|
2019-02-26 21:59:44 +00:00
|
|
|
randField, randVal := "field-"+randString(30), gun.ValueString(randString(30))
|
2019-02-26 19:06:16 +00:00
|
|
|
// Start a fetcher
|
|
|
|
ctx.debugf("Starting fetcher")
|
|
|
|
fetchGun := ctx.newGunConnectedToGunServer(remoteURL)
|
|
|
|
defer fetchGun.Close()
|
2019-02-26 21:59:44 +00:00
|
|
|
fetchCh := fetchGun.Scoped(ctx, "esgopeta-test", "TestGunPubSubSimpleRemote", randField).Fetch(ctx)
|
2019-02-26 19:06:16 +00:00
|
|
|
// Now put it from another instance
|
|
|
|
ctx.debugf("Putting data")
|
|
|
|
putGun := ctx.newGunConnectedToGunServer(remoteURL)
|
|
|
|
defer putGun.Close()
|
2019-02-26 21:59:44 +00:00
|
|
|
putScope := putGun.Scoped(ctx, "esgopeta-test", "TestGunPubSubSimpleRemote", randField)
|
2019-02-26 19:06:16 +00:00
|
|
|
putScope.Put(ctx, randVal)
|
|
|
|
ctx.debugf("Checking fetcher")
|
|
|
|
// See that the fetch got the value
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
ctx.Require.NoError(ctx.Err())
|
|
|
|
case result := <-fetchCh:
|
|
|
|
ctx.Require.NoError(result.Err)
|
|
|
|
if !result.ValueExists {
|
|
|
|
ctx.debugf("No value, trying again (got %v)", result)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
ctx.Require.Equal(randVal, result.Value)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-25 20:09:47 +00:00
|
|
|
/*
|
|
|
|
TODO Tests to write:
|
|
|
|
* test put w/ future state happens then
|
|
|
|
* test put w/ old state is discarded
|
|
|
|
* test put w/ new state is persisted
|
|
|
|
* test put w/ same state but greater is persisted
|
|
|
|
* test put w/ same state but less is discarded
|
|
|
|
*/
|