Skip to content

Commit

Permalink
Add generic state listener
Browse files Browse the repository at this point in the history
  • Loading branch information
kegsay committed Nov 22, 2023
1 parent 1e11907 commit d802345
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 16 deletions.
10 changes: 5 additions & 5 deletions internal/api/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ type Client interface {
MustBackpaginate(t *testing.T, 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 *testing.T, roomID, eventID string) Event
// MustBackupKeys will backup E2EE keys using the password provided, else fail the test.
MustBackupKeys(t *testing.T, password string)
// MustBackupKeys will backup E2EE keys, else fail the test.
MustBackupKeys(t *testing.T)
// Log something to stdout and the underlying client log file
Logf(t *testing.T, format string, args ...interface{})
// The user for this client
Expand Down Expand Up @@ -98,10 +98,10 @@ func (c *LoggedClient) MustBackpaginate(t *testing.T, roomID string, count int)
c.Client.MustBackpaginate(t, roomID, count)
}

func (c *LoggedClient) MustBackupKeys(t *testing.T, password string) {
func (c *LoggedClient) MustBackupKeys(t *testing.T) {
t.Helper()
c.Logf(t, "%s MustBackupKeys password=%s", c.logPrefix(), password)
c.Client.MustBackupKeys(t, password)
c.Logf(t, "%s MustBackupKeys", c.logPrefix())
c.Client.MustBackupKeys(t)
}

func (c *LoggedClient) logPrefix() string {
Expand Down
2 changes: 1 addition & 1 deletion internal/api/js.go
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ func (c *JSClient) MustBackpaginate(t *testing.T, roomID string, count int) {
))
}

func (c *JSClient) MustBackupKeys(t *testing.T, password string) {
func (c *JSClient) MustBackupKeys(t *testing.T) {

}

Expand Down
54 changes: 44 additions & 10 deletions internal/api/rust.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,12 +92,10 @@ func (c *RustClient) StartSyncing(t *testing.T) (stopSyncing func()) {
t.Helper()
syncService, err := c.FFIClient.SyncService().Finish()
must.NotError(t, fmt.Sprintf("[%s]failed to make sync service", c.userID), err)
ch := make(chan matrix_sdk_ffi.RoomListLoadingState, 10)
roomList, err := syncService.RoomListService().AllRooms()
must.NotError(t, "failed to call SyncService.RoomListService.AllRooms", err)
result, err := roomList.LoadingState(&roomListLoadingStateListener{
ch: ch,
})
genericListener := newGenericStateListener[matrix_sdk_ffi.RoomListLoadingState]()
result, err := roomList.LoadingState(genericListener)
must.NotError(t, "failed to call RoomList.LoadingState", err)
go syncService.Start()
c.allRooms = roomList
Expand All @@ -108,7 +106,7 @@ func (c *RustClient) StartSyncing(t *testing.T) (stopSyncing func()) {
select {
case <-time.After(5 * time.Second):
fatalf(t, "[%s](rust) timed out after 5s StartSyncing", c.userID)
case state := <-ch:
case state := <-genericListener.ch:
fmt.Println(state)
switch state.(type) {
case matrix_sdk_ffi.RoomListLoadingStateLoaded:
Expand All @@ -118,6 +116,7 @@ func (c *RustClient) StartSyncing(t *testing.T) (stopSyncing func()) {
}
}
}
genericListener.Close()

result.StateStream.Cancel()

Expand All @@ -139,7 +138,27 @@ func (c *RustClient) IsRoomEncrypted(t *testing.T, roomID string) (bool, error)
return r.IsEncrypted()
}

func (c *RustClient) MustBackupKeys(t *testing.T, password string) {
func (c *RustClient) MustBackupKeys(t *testing.T) {
// no-op, ffi does this by default
/*
t.Helper()
must.NotError(t, "failed to EnableBackups", c.FFIClient.Encryption().EnableBackups())
genericListener := newGenericStateListener[matrix_sdk_ffi.BackupUploadState]()
var listener matrix_sdk_ffi.BackupSteadyStateListener = genericListener
must.NotError(t, "failed to WaitForBackupUploadSteadyState", c.FFIClient.Encryption().WaitForBackupUploadSteadyState(&listener))
for s := range genericListener.ch {
switch x := s.(type) {
case matrix_sdk_ffi.BackupUploadStateWaiting:
c.Logf(t, "MustBackupKeys: state=waiting")
case matrix_sdk_ffi.BackupUploadStateUploading:
c.Logf(t, "MustBackupKeys: state=uploading %d/%d", x.BackedUpCount, x.TotalCount)
case matrix_sdk_ffi.BackupUploadStateError:
fatalf(t, "MustBackupKeys: state=error")
case matrix_sdk_ffi.BackupUploadStateDone:
genericListener.Close()
return
}
} */
}

func (c *RustClient) WaitUntilEventInRoom(t *testing.T, roomID string, checker func(Event) bool) Waiter {
Expand Down Expand Up @@ -494,10 +513,25 @@ func eventTimelineItemToEvent(item *matrix_sdk_ffi.EventTimelineItem) *Event {
return &complementEvent
}

type roomListLoadingStateListener struct {
ch chan matrix_sdk_ffi.RoomListLoadingState
type genericStateListener[T any] struct {
ch chan T
isClosed bool
}

func newGenericStateListener[T any]() *genericStateListener[T] {
return &genericStateListener[T]{
ch: make(chan T),
}
}

func (l *genericStateListener[T]) Close() {
l.isClosed = true
close(l.ch)
}

func (s *roomListLoadingStateListener) OnUpdate(state matrix_sdk_ffi.RoomListLoadingState) {
s.ch <- state
func (l *genericStateListener[T]) OnUpdate(state T) {
if l.isClosed {
return
}
l.ch <- state
}

0 comments on commit d802345

Please sign in to comment.