Skip to content
This repository has been archived by the owner on Dec 10, 2019. It is now read-only.

E2E Experiment #106

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 63 additions & 0 deletions e2e/e2e_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
//+build integration

package e2e

import (
"os"
"runtime"
"strconv"
"testing"

"github.com/fortytw2/hydrocarbon"
"github.com/fortytw2/hydrocarbon/pg"
)

type E2ETestCase struct {
Name string
Run func(browser *chromedp.Chrome, addr string, db *pg.DB, mm *hydrocarbon.MockMailer) error
}

var cases = []E2ETestCase{
{
// TODO: test cases go here
},
}

func TestEndToEnd(t *testing.T) {
var instances int
if val, ok := os.LookupEnv("E2E_PARALLELISM"); ok {
instances, err = strconv.Atoi(val)
if err != nil {
t.Fatal(err)
}
} else {
// this should be a sensible default
instances = runtime.NumCPU() / 2
}

pool := newTestServerPool(instances)
chromePool := chromedp.NewPool(instances)

for _, c := range cases {
t.Run(c.Name, func(t *testing.T) {
// TODO(fortytw2): does this work right with subtests
t.Parallel()

// this will block if there are no free instances
s, release := pool.getServer()
defer release()

chrome, chromeRelease := chromePool.GetInstance()
defer chromeRelease()

if c.Run != nil {
err := c.Run(chrome, s.addr, s.db, s.mm)
if err != nil {
t.Fatal(err)
}
}

s.db.TruncateTables()
})
}
}
62 changes: 62 additions & 0 deletions e2e/server_pool.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
//+build integration

package e2e

import (
"fmt"
"net/http"

"github.com/fortytw2/hydrocarbon"
"github.com/fortytw2/hydrocarbon/pg"
)

type testServer struct {
addr string
s http.Server
db *pg.DB
mm *hydrocarbon.MockMailer
}

type testServerPool struct {
servers chan *testServer
}

func newTestServerPool(instances int) *testServerPool {
var addrs []string
for index := 0; index < instances; index++ {
addrs = append(addrs, fmt.Sprintf(":690%d", index))
}

var servers []testServer
for _, addr := range addrs {
fullAddr := fmt.Sprintf("http://localhost%s", addr)

server, db, mockMailer, cancel := SetupE2EServer(t, fullAddr)
defer cancel()

servers = append(servers, testServer{
addr: fullAddr,
s: server,
db: db,
mm: mockMailer,
})

go http.ListenAndServe(addr, server)
}

serverChan := make(chan *testServer, len(servers))
for _, s := range servers {
serverChan <- s
}

return &testServerPool{
servers: serverChan,
}
}

func (t *testServerPool) getServer() (*testServer, func()) {
s := <-t.servers
return s, func() {
t.servers <- s
}
}
39 changes: 39 additions & 0 deletions e2e/setup_e2e_server.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
//+build integration

package e2e

import (
"net/http"
"testing"

"github.com/fortytw2/hydrocarbon"
"github.com/fortytw2/hydrocarbon/discollect"
)

func SetupE2EServer(t *testing.T, address string) (http.Server, *pg.DB, *hydrocarbon.MockMailer, func()) {
t.Helper()

db, cancel := pg.SetupTestDB()

dc, _ := discollect.New(discollect.WithPlugins(&discollect.Plugin{
Name: "ycombinators",
Entrypoints: []string{".*"},
ConfigCreator: func(url string, ho *discollect.HandlerOpts) (string, *discollect.Config, error) {
return "gotem", &discollect.Config{
Type: discollect.FullScrape,
Entrypoints: []string{"gotem"},
}, nil
},
}))

mm := &hydrocarbon.MockMailer{}
ks := hydrocarbon.NewKeySigner("test")
h := hydrocarbon.NewRouter(
hydrocarbon.NewUserAPI(db, ks, mm, "", "", false),
hydrocarbon.NewFeedAPI(db, dc, ks),
hydrocarbon.NewReadStatusAPI(db, ks),
address,
)

return h, db, mm, cancel
}