Skip to content

Commit

Permalink
refactor: completely redo how logging is done
Browse files Browse the repository at this point in the history
- We now have `container-hs1.log` and `container-hs2.log` written when
  `COMPLEMENT_CRYPTO_WRITE_CONTAINER_LOGS=1`.
- Use the interfaces from Complement and remove handcrafted interfaces.
- Use Complement `must` package again.
  • Loading branch information
kegsay committed Jan 17, 2024
1 parent 434a393 commit f3ba378
Show file tree
Hide file tree
Showing 17 changed files with 170 additions and 252 deletions.
3 changes: 1 addition & 2 deletions .github/workflows/tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -120,5 +120,4 @@ jobs:
with:
name: Logs - ${{ job.status }}
path: |
./tests/rust_sdk_logs*
./tests/*.log
./tests/logs/*
6 changes: 3 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ require (
github.com/chromedp/cdproto v0.0.0-20231025043423-5615e204d422
github.com/chromedp/chromedp v0.9.3
github.com/docker/go-connections v0.4.0
github.com/matrix-org/complement v0.0.0-20231121123151-a43fb99237ed
github.com/matrix-org/complement v0.0.0-20240117130049-f24331b29b64
github.com/testcontainers/testcontainers-go v0.26.0
github.com/tidwall/gjson v1.16.0
golang.org/x/exp v0.0.0-20230905200255-921286631fa9
Expand Down Expand Up @@ -58,10 +58,10 @@ require (
github.com/tklauser/go-sysconf v0.3.12 // indirect
github.com/tklauser/numcpus v0.6.1 // indirect
github.com/yusufpapurcu/wmi v1.2.3 // indirect
golang.org/x/crypto v0.14.0 // indirect
golang.org/x/crypto v0.17.0 // indirect
golang.org/x/mod v0.12.0 // indirect
golang.org/x/net v0.17.0 // indirect
golang.org/x/sys v0.13.0 // indirect
golang.org/x/sys v0.15.0 // indirect
golang.org/x/tools v0.13.0 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20230525234030-28d5490b6b19 // indirect
google.golang.org/grpc v1.57.1 // indirect
Expand Down
8 changes: 8 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,10 @@ github.com/mailru/easyjson v0.7.7 h1:UGYAvKxe3sBsEDzO8ZeWOSlIQfWFlxbzLZe7hwFURr0
github.com/mailru/easyjson v0.7.7/go.mod h1:xzfreul335JAWq5oZzymOObrkdz5UnU4kGfJJLY9Nlc=
github.com/matrix-org/complement v0.0.0-20231121123151-a43fb99237ed h1:sGEyUnlj9tNYzAvIq9P/7pTQCQz+KwnQltLvOxuNA10=
github.com/matrix-org/complement v0.0.0-20231121123151-a43fb99237ed/go.mod h1:MpFg8YqIgcfReuwX5x0HyIsxkekKyUvbXxY6ey9LeE0=
github.com/matrix-org/complement v0.0.0-20240117124104-5ecb086412c1 h1:YryEz+vrdtun3l0qqggeB75sJ0WyEJmXzE2zxMrzhqs=
github.com/matrix-org/complement v0.0.0-20240117124104-5ecb086412c1/go.mod h1:GMCwbgMOudedB86u1c5+nfQS1L31sFHZ9/YzTYqWyjU=
github.com/matrix-org/complement v0.0.0-20240117130049-f24331b29b64 h1:dBvjM8idvlfkgyCeimfSxZTXdE85wdfJfGIEPq4GAu8=
github.com/matrix-org/complement v0.0.0-20240117130049-f24331b29b64/go.mod h1:GMCwbgMOudedB86u1c5+nfQS1L31sFHZ9/YzTYqWyjU=
github.com/matrix-org/gomatrix v0.0.0-20220926102614-ceba4d9f7530 h1:kHKxCOLcHH8r4Fzarl4+Y3K5hjothkVW5z7T1dUM11U=
github.com/matrix-org/gomatrix v0.0.0-20220926102614-ceba4d9f7530/go.mod h1:/gBX06Kw0exX1HrwmoBibFA98yBk/jxKpGVeyQbff+s=
github.com/matrix-org/gomatrixserverlib v0.0.0-20230921171121-0466775328c7 h1:NhPNNFLHwdDb/upeicBh1GkxX/sFinEp5HF1WBqPtiY=
Expand Down Expand Up @@ -168,6 +172,8 @@ golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8U
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/crypto v0.14.0 h1:wBqGXzWJW6m1XrIKlAH0Hs1JJ7+9KBwnIO8v66Q9cHc=
golang.org/x/crypto v0.14.0/go.mod h1:MVFd36DqK4CsrnJYDkBA3VC4m2GkXAM0PvzMCn4JQf4=
golang.org/x/crypto v0.17.0 h1:r8bRNjWL3GshPW3gkd+RpvzWrZAwPS49OmTGZ/uhM4k=
golang.org/x/crypto v0.17.0/go.mod h1:gCAAfMLgwOJRpTjQ2zCCt2OcSfYMTeZVSRtQlPC7Nq4=
golang.org/x/exp v0.0.0-20230905200255-921286631fa9 h1:GoHiUyI/Tp2nVkLI2mCxVkOjsbSXD66ic0XW0js0R9g=
golang.org/x/exp v0.0.0-20230905200255-921286631fa9/go.mod h1:S2oDrQGGwySpoQPVqRShND87VCbxmc6bL1Yd2oYrm6k=
golang.org/x/mod v0.2.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
Expand Down Expand Up @@ -206,6 +212,8 @@ golang.org/x/sys v0.11.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.13.0 h1:Af8nKPmuFypiUBjVoU9V20FiaFXOcuZI21p0ycVYYGE=
golang.org/x/sys v0.13.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.15.0 h1:h48lPFYpsTvQJZF4EKyI4aLHaev3CxivZmv7yZig9pc=
golang.org/x/sys v0.15.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
Expand Down
55 changes: 28 additions & 27 deletions internal/api/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"time"

"github.com/matrix-org/complement/client"
"github.com/matrix-org/complement/ct"
)

type ClientTypeLang string
Expand All @@ -26,41 +27,41 @@ type Client interface {
// Specifically, we need to shut off existing browsers and any FFI bindings.
// If we get callbacks/events after this point, tests may panic if the callbacks
// log messages.
Close(t Test)
Close(t ct.TestLike)
// Remove any persistent storage, if it was enabled.
DeletePersistentStorage(t Test)
Login(t Test, opts ClientCreationOpts) error
DeletePersistentStorage(t ct.TestLike)
Login(t ct.TestLike, opts ClientCreationOpts) error
// MustStartSyncing to begin syncing from sync v2 / sliding sync.
// Tests should call stopSyncing() at the end of the test.
// MUST BLOCK until the initial sync is complete.
// Fails the test if there was a problem syncing.
MustStartSyncing(t Test) (stopSyncing func())
MustStartSyncing(t ct.TestLike) (stopSyncing func())
// StartSyncing to begin syncing from sync v2 / sliding sync.
// Tests should call stopSyncing() at the end of the test.
// MUST BLOCK until the initial sync is complete.
// Returns an error if there was a problem syncing.
StartSyncing(t Test) (stopSyncing func(), err error)
StartSyncing(t ct.TestLike) (stopSyncing func(), err error)
// IsRoomEncrypted returns true if the room is encrypted. May return an error e.g if you
// provide a bogus room ID.
IsRoomEncrypted(t Test, roomID string) (bool, error)
IsRoomEncrypted(t ct.TestLike, roomID string) (bool, error)
// SendMessage sends the given text as an m.room.message with msgtype:m.text into the given
// room. Returns the event ID of the sent event, so MUST BLOCK until the event has been sent.
SendMessage(t Test, roomID, text string) (eventID string)
SendMessage(t ct.TestLike, roomID, text string) (eventID string)
// TrySendMessage tries to send the message, but can fail.
TrySendMessage(t Test, roomID, text string) (eventID string, err error)
TrySendMessage(t ct.TestLike, roomID, text string) (eventID string, err error)
// Wait until an event with the given body is seen. Not all impls expose event IDs
// hence needing to use body as a proxy.
WaitUntilEventInRoom(t Test, roomID string, checker func(e Event) bool) Waiter
WaitUntilEventInRoom(t ct.TestLike, roomID string, checker func(e Event) bool) Waiter
// Backpaginate in this room by `count` events.
MustBackpaginate(t Test, roomID string, count int)
MustBackpaginate(t ct.TestLike, roomID string, count int)
// MustGetEvent will return the client's view of this event, or fail the test if the event cannot be found.
MustGetEvent(t Test, roomID, eventID string) Event
MustGetEvent(t ct.TestLike, roomID, eventID string) Event
// MustBackupKeys will backup E2EE keys, else fail the test.
MustBackupKeys(t Test) (recoveryKey string)
MustBackupKeys(t ct.TestLike) (recoveryKey string)
// MustLoadBackup will recover E2EE keys from the latest backup, else fail the test.
MustLoadBackup(t Test, recoveryKey string)
MustLoadBackup(t ct.TestLike, recoveryKey string)
// Log something to stdout and the underlying client log file
Logf(t Test, format string, args ...interface{})
Logf(t ct.TestLike, format string, args ...interface{})
// The user for this client
UserID() string
Type() ClientTypeLang
Expand All @@ -70,83 +71,83 @@ type LoggedClient struct {
Client
}

func (c *LoggedClient) Login(t Test, opts ClientCreationOpts) error {
func (c *LoggedClient) Login(t ct.TestLike, opts ClientCreationOpts) error {
t.Helper()
c.Logf(t, "%s Login %+v", c.logPrefix(), opts)
return c.Client.Login(t, opts)
}

func (c *LoggedClient) Close(t Test) {
func (c *LoggedClient) Close(t ct.TestLike) {
t.Helper()
c.Logf(t, "%s Close", c.logPrefix())
c.Client.Close(t)
}

func (c *LoggedClient) MustStartSyncing(t Test) (stopSyncing func()) {
func (c *LoggedClient) MustStartSyncing(t ct.TestLike) (stopSyncing func()) {
t.Helper()
c.Logf(t, "%s MustStartSyncing starting to sync", c.logPrefix())
stopSyncing = c.Client.MustStartSyncing(t)
c.Logf(t, "%s MustStartSyncing now syncing", c.logPrefix())
return
}

func (c *LoggedClient) StartSyncing(t Test) (stopSyncing func(), err error) {
func (c *LoggedClient) StartSyncing(t ct.TestLike) (stopSyncing func(), err error) {
t.Helper()
c.Logf(t, "%s StartSyncing starting to sync", c.logPrefix())
stopSyncing, err = c.Client.StartSyncing(t)
c.Logf(t, "%s StartSyncing now syncing", c.logPrefix())
return
}

func (c *LoggedClient) IsRoomEncrypted(t Test, roomID string) (bool, error) {
func (c *LoggedClient) IsRoomEncrypted(t ct.TestLike, roomID string) (bool, error) {
t.Helper()
c.Logf(t, "%s IsRoomEncrypted %s", c.logPrefix(), roomID)
return c.Client.IsRoomEncrypted(t, roomID)
}

func (c *LoggedClient) TrySendMessage(t Test, roomID, text string) (eventID string, err error) {
func (c *LoggedClient) TrySendMessage(t ct.TestLike, roomID, text string) (eventID string, err error) {
t.Helper()
c.Logf(t, "%s TrySendMessage %s => %s", c.logPrefix(), roomID, text)
eventID, err = c.Client.TrySendMessage(t, roomID, text)
c.Logf(t, "%s TrySendMessage %s => %s", c.logPrefix(), roomID, eventID)
return
}

func (c *LoggedClient) SendMessage(t Test, roomID, text string) (eventID string) {
func (c *LoggedClient) SendMessage(t ct.TestLike, roomID, text string) (eventID string) {
t.Helper()
c.Logf(t, "%s SendMessage %s => %s", c.logPrefix(), roomID, text)
eventID = c.Client.SendMessage(t, roomID, text)
c.Logf(t, "%s SendMessage %s => %s", c.logPrefix(), roomID, eventID)
return
}

func (c *LoggedClient) WaitUntilEventInRoom(t Test, roomID string, checker func(e Event) bool) Waiter {
func (c *LoggedClient) WaitUntilEventInRoom(t ct.TestLike, roomID string, checker func(e Event) bool) Waiter {
t.Helper()
c.Logf(t, "%s WaitUntilEventInRoom %s", c.logPrefix(), roomID)
return c.Client.WaitUntilEventInRoom(t, roomID, checker)
}

func (c *LoggedClient) MustBackpaginate(t Test, roomID string, count int) {
func (c *LoggedClient) MustBackpaginate(t ct.TestLike, roomID string, count int) {
t.Helper()
c.Logf(t, "%s MustBackpaginate %d %s", c.logPrefix(), count, roomID)
c.Client.MustBackpaginate(t, roomID, count)
}

func (c *LoggedClient) MustBackupKeys(t Test) (recoveryKey string) {
func (c *LoggedClient) MustBackupKeys(t ct.TestLike) (recoveryKey string) {
t.Helper()
c.Logf(t, "%s MustBackupKeys", c.logPrefix())
recoveryKey = c.Client.MustBackupKeys(t)
c.Logf(t, "%s MustBackupKeys => %s", c.logPrefix(), recoveryKey)
return recoveryKey
}

func (c *LoggedClient) MustLoadBackup(t Test, recoveryKey string) {
func (c *LoggedClient) MustLoadBackup(t ct.TestLike, recoveryKey string) {
t.Helper()
c.Logf(t, "%s MustLoadBackup key=%s", c.logPrefix(), recoveryKey)
c.Client.MustLoadBackup(t, recoveryKey)
}

func (c *LoggedClient) DeletePersistentStorage(t Test) {
func (c *LoggedClient) DeletePersistentStorage(t ct.TestLike) {
t.Helper()
c.Logf(t, "%s DeletePersistentStorage", c.logPrefix())
c.Client.DeletePersistentStorage(t)
Expand Down Expand Up @@ -196,7 +197,7 @@ type Event struct {
}

type Waiter interface {
Wait(t Test, s time.Duration)
Wait(t ct.TestLike, s time.Duration)
}

func CheckEventHasBody(body string) func(e Event) bool {
Expand Down
9 changes: 4 additions & 5 deletions internal/api/js/chrome/chrome.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (

"github.com/chromedp/cdproto/runtime"
"github.com/chromedp/chromedp"
"github.com/matrix-org/complement-crypto/internal/api"
"github.com/matrix-org/complement/ct"
)

//go:embed dist
Expand All @@ -36,7 +36,7 @@ type Void *runtime.RemoteObject
//
// result, err := RunAsyncFn[string](t, ctx, "return await getSomeString()")
// void, err := RunAsyncFn[chrome.Void](t, ctx, "doSomething(); await doSomethingElse();")
func RunAsyncFn[T any](t api.Test, ctx context.Context, js string) (*T, error) {
func RunAsyncFn[T any](t ct.TestLike, ctx context.Context, js string) (*T, error) {
t.Helper()
out := new(T)
err := chromedp.Run(ctx,
Expand All @@ -55,11 +55,11 @@ func RunAsyncFn[T any](t api.Test, ctx context.Context, js string) (*T, error) {
// Run an anonymous async iffe in the browser. Set the type parameter to a basic data type
// which can be returned as JSON e.g string, map[string]any, []string. If you do not want
// to return anything, use chrome.Void
func MustRunAsyncFn[T any](t api.Test, ctx context.Context, js string) *T {
func MustRunAsyncFn[T any](t ct.TestLike, ctx context.Context, js string) *T {
t.Helper()
result, err := RunAsyncFn[T](t, ctx, js)
if err != nil {
api.Fatalf(t, "MustRunAsyncFn: %s", err)
ct.Fatalf(t, "MustRunAsyncFn: %s", err)
}
return result
}
Expand All @@ -86,7 +86,6 @@ func RunHeadless(onConsoleLog func(s string), requiresPersistance bool, listenPo
opts = append(opts,
chromedp.UserDataDir(userDir),
)
fmt.Println(userDir)
}

allocCtx, allocCancel := chromedp.NewExecAllocator(context.Background(), opts...)
Expand Down
Loading

0 comments on commit f3ba378

Please sign in to comment.