mirror of
https://github.com/zitadel/zitadel.git
synced 2024-12-14 20:08:02 +00:00
d2e0ac07f1
# Which Problems Are Solved Use a single server instance for API integration tests. This optimizes the time taken for the integration test pipeline, because it allows running tests on multiple packages in parallel. Also, it saves time by not start and stopping a zitadel server for every package. # How the Problems Are Solved - Build a binary with `go build -race -cover ....` - Integration tests only construct clients. The server remains running in the background. - The integration package and tested packages now fully utilize the API. No more direct database access trough `query` and `command` packages. - Use Makefile recipes to setup, start and stop the server in the background. - The binary has the race detector enabled - Init and setup jobs are configured to halt immediately on race condition - Because the server runs in the background, races are only logged. When the server is stopped and race logs exist, the Makefile recipe will throw an error and print the logs. - Makefile recipes include logic to print logs and convert coverage reports after the server is stopped. - Some tests need a downstream HTTP server to make requests, like quota and milestones. A new `integration/sink` package creates an HTTP server and uses websockets to forward HTTP request back to the test packages. The package API uses Go channels for abstraction and easy usage. # Additional Changes - Integration test files already used the `//go:build integration` directive. In order to properly split integration from unit tests, integration test files need to be in a `integration_test` subdirectory of their package. - `UseIsolatedInstance` used to overwrite the `Tester.Client` for each instance. Now a `Instance` object is returned with a gRPC client that is connected to the isolated instance's hostname. - The `Tester` type is now `Instance`. The object is created for the first instance, used by default in any test. Isolated instances are also `Instance` objects and therefore benefit from the same methods and values. The first instance and any other us capable of creating an isolated instance over the system API. - All test packages run in an Isolated instance by calling `NewInstance()` - Individual tests that use an isolated instance use `t.Parallel()` # Additional Context - Closes #6684 - https://go.dev/doc/articles/race_detector - https://go.dev/doc/build-cover --------- Co-authored-by: Stefan Benz <46600784+stebenz@users.noreply.github.com>
91 lines
1.9 KiB
Go
91 lines
1.9 KiB
Go
//go:build integration
|
|
|
|
package sink
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
"io"
|
|
"net"
|
|
"net/http"
|
|
"net/url"
|
|
"sync/atomic"
|
|
|
|
"github.com/gorilla/websocket"
|
|
"github.com/zitadel/logging"
|
|
)
|
|
|
|
// Request is a message forwarded from the handler to [Subscription]s.
|
|
type Request struct {
|
|
Header http.Header
|
|
Body json.RawMessage
|
|
}
|
|
|
|
// Subscription is a websocket client to which [Request]s are forwarded by the server.
|
|
type Subscription struct {
|
|
conn *websocket.Conn
|
|
closed atomic.Bool
|
|
reqChannel chan *Request
|
|
}
|
|
|
|
// Subscribe to a channel.
|
|
// The subscription forwards all requests it received on the channel's
|
|
// handler, after Subscribe has returned.
|
|
// Multiple subscription may be active on a single channel.
|
|
// Each request is always forwarded to each Subscription.
|
|
// Close must be called to cleanup up the Subscription's channel and go routine.
|
|
func Subscribe(ctx context.Context, ch Channel) *Subscription {
|
|
u := url.URL{
|
|
Scheme: "ws",
|
|
Host: listenAddr,
|
|
Path: subscribePath(ch),
|
|
}
|
|
conn, resp, err := websocket.DefaultDialer.DialContext(ctx, u.String(), nil)
|
|
if err != nil {
|
|
if resp != nil {
|
|
defer resp.Body.Close()
|
|
body, _ := io.ReadAll(resp.Body)
|
|
err = fmt.Errorf("subscribe: %w, status: %s, body: %s", err, resp.Status, body)
|
|
}
|
|
panic(err)
|
|
}
|
|
|
|
sub := &Subscription{
|
|
conn: conn,
|
|
reqChannel: make(chan *Request, 10),
|
|
}
|
|
go sub.readToChan()
|
|
return sub
|
|
}
|
|
|
|
func (s *Subscription) readToChan() {
|
|
for {
|
|
if s.closed.Load() {
|
|
break
|
|
}
|
|
req := new(Request)
|
|
if err := s.conn.ReadJSON(req); err != nil {
|
|
opErr := new(net.OpError)
|
|
if errors.As(err, &opErr) {
|
|
break
|
|
}
|
|
logging.WithError(err).Error("subscription read")
|
|
break
|
|
}
|
|
s.reqChannel <- req
|
|
}
|
|
close(s.reqChannel)
|
|
}
|
|
|
|
// Recv returns the channel over which [Request]s are send.
|
|
func (s *Subscription) Recv() <-chan *Request {
|
|
return s.reqChannel
|
|
}
|
|
|
|
func (s *Subscription) Close() error {
|
|
s.closed.Store(true)
|
|
return s.conn.Close()
|
|
}
|