github.com/keybase/client/go@v0.0.0-20241007131713-f10651d043c8/chat/participantsource_test.go (about)

     1  package chat
     2  
     3  import (
     4  	"context"
     5  	"testing"
     6  	"time"
     7  
     8  	"github.com/keybase/client/go/chat/types"
     9  	"github.com/keybase/client/go/chat/utils"
    10  	"github.com/keybase/client/go/protocol/chat1"
    11  	"github.com/keybase/client/go/protocol/gregor1"
    12  	"github.com/keybase/client/go/teams"
    13  	"github.com/stretchr/testify/require"
    14  )
    15  
    16  func TestParticipantsSource(t *testing.T) {
    17  	useRemoteMock = false
    18  	defer func() { useRemoteMock = true }()
    19  	ctc := makeChatTestContext(t, "TestParticipantsSource", 3)
    20  	defer ctc.cleanup()
    21  
    22  	timeout := time.Second * 20
    23  	users := ctc.users()
    24  	tc := ctc.world.Tcs[users[0].Username]
    25  	ctx := ctc.as(t, users[0]).startCtx
    26  	uid := gregor1.UID(users[0].GetUID().ToBytes())
    27  	listener0 := newServerChatListener()
    28  	ctc.as(t, users[0]).h.G().NotifyRouter.AddListener(listener0)
    29  
    30  	info := mustCreateConversationForTest(t, ctc, users[0], chat1.TopicType_CHAT,
    31  		chat1.ConversationMembersType_TEAM, users[1])
    32  	conv, err := utils.GetUnverifiedConv(ctx, tc.Context(), uid, info.Id, types.InboxSourceDataSourceAll)
    33  	require.NoError(t, err)
    34  
    35  	// empty should get one
    36  	ch := tc.Context().ParticipantsSource.GetNonblock(ctx, uid, conv.GetConvID(),
    37  		types.InboxSourceDataSourceAll)
    38  	select {
    39  	case pres := <-ch:
    40  		require.NoError(t, pres.Err)
    41  		require.Equal(t, 2, len(pres.Uids))
    42  	case <-time.After(timeout):
    43  		require.Fail(t, "no uids")
    44  	}
    45  	time.Sleep(time.Millisecond * 200)
    46  	_, ok := <-ch
    47  	require.False(t, ok)
    48  
    49  	// cached should get one
    50  	ch = tc.Context().ParticipantsSource.GetNonblock(ctx, uid, conv.GetConvID(),
    51  		types.InboxSourceDataSourceAll)
    52  	select {
    53  	case pres := <-ch:
    54  		require.NoError(t, pres.Err)
    55  		require.Equal(t, 2, len(pres.Uids))
    56  	case <-time.After(timeout):
    57  		require.Fail(t, "no uids")
    58  	}
    59  	time.Sleep(time.Millisecond * 200)
    60  	_, ok = <-ch
    61  	require.False(t, ok)
    62  
    63  	// hash wrong, should get two
    64  	err = teams.SetRoleWriter(context.TODO(), tc.G, info.TlfName, users[2].Username)
    65  	require.NoError(t, err)
    66  	consumeMembersUpdate(t, listener0)
    67  	ch = tc.Context().ParticipantsSource.GetNonblock(ctx, uid, conv.GetConvID(),
    68  		types.InboxSourceDataSourceAll)
    69  	select {
    70  	case pres := <-ch:
    71  		require.NoError(t, pres.Err)
    72  		require.Equal(t, 2, len(pres.Uids))
    73  	case <-time.After(timeout):
    74  		require.Fail(t, "no uids")
    75  	}
    76  	select {
    77  	case pres := <-ch:
    78  		require.NoError(t, pres.Err)
    79  		require.Equal(t, 3, len(pres.Uids))
    80  	case <-time.After(timeout):
    81  		require.Fail(t, "no uids")
    82  	}
    83  
    84  	// cached should get one
    85  	ch = tc.Context().ParticipantsSource.GetNonblock(ctx, uid, conv.GetConvID(),
    86  		types.InboxSourceDataSourceAll)
    87  	select {
    88  	case pres := <-ch:
    89  		require.NoError(t, pres.Err)
    90  		require.Equal(t, 3, len(pres.Uids))
    91  	case <-time.After(timeout):
    92  		require.Fail(t, "no uids")
    93  	}
    94  	time.Sleep(time.Millisecond * 200)
    95  	_, ok = <-ch
    96  	require.False(t, ok)
    97  }