Skip to content

Commit

Permalink
test: Do not rely on delay in import/export tests. Remove freeport.
Browse files Browse the repository at this point in the history
  • Loading branch information
dennwc committed Jul 26, 2023
1 parent 32c0235 commit 1310f3b
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 44 deletions.
42 changes: 19 additions & 23 deletions cmd/cayleyexport/cayleyexport_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,12 @@ package main
import (
"bytes"
"fmt"
"net"
"net/http"
"testing"
"time"

"github.com/cayleygraph/quad"
"github.com/cayleygraph/quad/jsonld"
"github.com/phayes/freeport"
"github.com/stretchr/testify/require"

"github.com/cayleygraph/cayley/graph"
Expand All @@ -27,40 +26,37 @@ var testData = []quad.Quad{
}

func serializeTestData() string {
buffer := bytes.NewBuffer(nil)
writer := jsonld.NewWriter(buffer)
writer.WriteQuads(testData)
writer.Close()
return buffer.String()
buf := bytes.NewBuffer(nil)
w := jsonld.NewWriter(buf)
w.WriteQuads(testData)
w.Close()
return buf.String()
}

func serve(addr string) {
func TestCayleyExport(t *testing.T) {
qs := memstore.New(testData...)
qw, err := graph.NewQuadWriter("single", qs, graph.Options{})
if err != nil {
panic(err)
}
require.NoError(t, err)
h := &graph.Handle{QuadStore: qs, QuadWriter: qw}
chttp.SetupRoutes(h, &chttp.Config{})
err = http.ListenAndServe(addr, nil)
if err != nil {
panic(err)
}
}

func TestCayleyExport(t *testing.T) {
port, err := freeport.GetFreePort()
lis, err := net.Listen("tcp", "127.0.0.1:0")
require.NoError(t, err)
addr := fmt.Sprintf("127.0.0.1:%d", port)
uri := fmt.Sprintf("http://%s", addr)
go serve(addr)
time.Sleep(time.Second / 2)
t.Cleanup(func() {
lis.Close()
})

srv := &http.Server{
Addr: lis.Addr().String(),
}
go srv.Serve(lis)

cmd := NewCmd()
b := bytes.NewBufferString("")
cmd.SetOut(b)
cmd.SetArgs([]string{
"--uri",
uri,
fmt.Sprintf("http://%s", lis.Addr().String()),
})
err = cmd.Execute()
require.NoError(t, err)
Expand Down
59 changes: 41 additions & 18 deletions cmd/cayleyimport/cayleyimport_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,51 +2,74 @@ package main

import (
"bytes"
"context"
"fmt"
"net"
"net/http"
"path"
"testing"
"time"

"github.com/phayes/freeport"
"github.com/cayleygraph/quad"
"github.com/stretchr/testify/require"

"github.com/cayleygraph/cayley/graph"
"github.com/cayleygraph/cayley/graph/memstore"
chttp "github.com/cayleygraph/cayley/internal/http"
)

func serve(addr string) {
var expectData = []quad.Quad{
{quad.IRI("http://example.com/alice"), quad.IRI("http://www.w3.org/1999/02/22-rdf-syntax-ns#type"), quad.IRI("http://xmlns.com/foaf/0.1/Person"), quad.Value(nil)},
{quad.IRI("http://example.com/alice"), quad.IRI("http://xmlns.com/foaf/0.1/knows"), quad.IRI("http://example.com/bob"), nil},
{quad.IRI("http://example.com/alice"), quad.IRI("http://xmlns.com/foaf/0.1/name"), quad.String("Alice"), nil},
{quad.IRI("http://example.com/bob"), quad.IRI("http://www.w3.org/1999/02/22-rdf-syntax-ns#type"), quad.IRI("http://xmlns.com/foaf/0.1/Person"), nil},
{quad.IRI("http://example.com/bob"), quad.IRI("http://xmlns.com/foaf/0.1/knows"), quad.IRI("http://example.com/alice"), nil},
{quad.IRI("http://example.com/bob"), quad.IRI("http://xmlns.com/foaf/0.1/name"), quad.String("Bob"), nil},
}

func allQuads(t testing.TB, qs graph.QuadStore) []quad.Quad {
ctx := context.Background()
it := qs.QuadsAllIterator().Iterate()
defer it.Close()
var out []quad.Quad
for it.Next(ctx) {
ref := it.Result()
q, err := qs.Quad(ref)
require.NoError(t, err)
out = append(out, q)
}
require.NoError(t, it.Err())
return out
}

func TestCayleyImport(t *testing.T) {
qs := memstore.New()
qw, err := graph.NewQuadWriter("single", qs, graph.Options{})
if err != nil {
panic(err)
}
require.NoError(t, err)
h := &graph.Handle{QuadStore: qs, QuadWriter: qw}
chttp.SetupRoutes(h, &chttp.Config{})
err = http.ListenAndServe(addr, nil)
if err != nil {
panic(err)
}
}

func TestCayleyImport(t *testing.T) {
port, err := freeport.GetFreePort()
lis, err := net.Listen("tcp", "127.0.0.1:0")
require.NoError(t, err)
addr := fmt.Sprintf("127.0.0.1:%d", port)
uri := fmt.Sprintf("http://%s", addr)
go serve(addr)
time.Sleep(time.Second / 2)
t.Cleanup(func() {
lis.Close()
})

srv := &http.Server{
Addr: lis.Addr().String(),
}
go srv.Serve(lis)

cmd := NewCmd()
b := bytes.NewBufferString("")
cmd.SetOut(b)
fileName := path.Join("..", "..", "data", "people.jsonld")
cmd.SetArgs([]string{
fileName,
"--uri",
uri,
fmt.Sprintf("http://%s", lis.Addr().String()),
})
err = cmd.Execute()
require.NoError(t, err)
require.Empty(t, b.String())
require.Equal(t, expectData, allQuads(t, qs))
}
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ require (
github.com/lib/pq v1.10.9
github.com/mattn/go-sqlite3 v1.14.17
github.com/peterh/liner v1.2.2
github.com/phayes/freeport v0.0.0-20220201140144-74d24b5ae9f5
github.com/piprate/json-gold v0.3.0
github.com/prometheus/client_golang v1.16.0
github.com/spf13/cobra v1.7.0
Expand Down
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -544,8 +544,6 @@ github.com/pelletier/go-toml/v2 v2.0.9 h1:uH2qQXheeefCCkuBBSLi7jCiSmj3VRh2+Goq2N
github.com/pelletier/go-toml/v2 v2.0.9/go.mod h1:tJU2Z3ZkXwnxa4DPO899bsyIoywizdUvyaeZurnPPDc=
github.com/peterh/liner v1.2.2 h1:aJ4AOodmL+JxOZZEL2u9iJf8omNRpqHc/EbrK+3mAXw=
github.com/peterh/liner v1.2.2/go.mod h1:xFwJyiKIXJZUKItq5dGHZSTBRAuG/CpeNpWLyiNRNwI=
github.com/phayes/freeport v0.0.0-20220201140144-74d24b5ae9f5 h1:Ii+DKncOVM8Cu1Hc+ETb5K+23HdAMvESYE3ZJ5b5cMI=
github.com/phayes/freeport v0.0.0-20220201140144-74d24b5ae9f5/go.mod h1:iIss55rKnNBTvrwdmkUpLnDpZoAHvWaiq5+iMmen4AE=
github.com/pingcap/errors v0.11.4 h1:lFuQV/oaUMGcD2tqt+01ROSmJs75VG1ToEOkZIZ4nE4=
github.com/pingcap/errors v0.11.4/go.mod h1:Oi8TUi2kEtXXLMJk9l1cGmz20kV3TaQ0usTwv5KuLY8=
github.com/piprate/json-gold v0.3.0 h1:a1vHx7Q1jOO1pjCtKwTI/WCzwaQwRt9VM7apK2uy200=
Expand Down

0 comments on commit 1310f3b

Please sign in to comment.