github.com/status-im/status-go@v1.1.0/protocol/messenger_community_for_mobile_testing_test.go (about)

     1  package protocol
     2  
     3  import (
     4  	"errors"
     5  	"sync"
     6  	"testing"
     7  	"time"
     8  
     9  	"github.com/stretchr/testify/suite"
    10  
    11  	"github.com/status-im/status-go/eth-node/types"
    12  	"github.com/status-im/status-go/protocol/tt"
    13  )
    14  
    15  type MessengerCommunityForMobileTestingTestSuite struct {
    16  	MessengerBaseTestSuite
    17  }
    18  
    19  func TestMessengerCommunityForMobileTesting(t *testing.T) {
    20  	suite.Run(t, new(MessengerCommunityForMobileTestingTestSuite))
    21  }
    22  
    23  func (s *MessengerCommunityForMobileTestingTestSuite) SetupTest() {
    24  	s.MessengerBaseTestSuite.SetupTest()
    25  }
    26  
    27  func (s *MessengerCommunityForMobileTestingTestSuite) TearDownTest() {
    28  	s.MessengerBaseTestSuite.TearDownTest()
    29  }
    30  
    31  func (s *MessengerCommunityForMobileTestingTestSuite) TestCreateClosedCommunity() {
    32  	var wg sync.WaitGroup
    33  	wg.Add(1)
    34  	// simulate invoking `HandleCommunityDescription`
    35  	go func() {
    36  		err := tt.RetryWithBackOff(func() error {
    37  			r, err := s.m.RetrieveAll()
    38  			s.Require().NoError(err)
    39  			if len(r.Communities()) > 0 {
    40  				return nil
    41  			}
    42  			return errors.New("not receive enough messages relate to community")
    43  		})
    44  		wg.Done()
    45  		s.Require().NoError(err)
    46  	}()
    47  
    48  	wg.Add(1)
    49  	var communityID types.HexBytes
    50  	// simulate frontend(mobile) invoking `CreateClosedCommunity`
    51  	go func() {
    52  		response, err := s.m.CreateClosedCommunity()
    53  		s.Require().NoError(err)
    54  		s.Require().Len(response.Communities(), 1)
    55  		s.Require().Len(response.Communities()[0].Categories(), 2)
    56  		s.Require().Len(response.Chats(), 4)
    57  		s.Require().Len(response.Communities()[0].Description().Chats, 4)
    58  		communityID = response.Communities()[0].ID()
    59  		wg.Done()
    60  	}()
    61  
    62  	timeout := time.After(20 * time.Second)
    63  	done := make(chan bool)
    64  	go func() {
    65  		wg.Wait()
    66  		close(done)
    67  	}()
    68  
    69  	select {
    70  	case <-timeout:
    71  		s.Fail("TestCreateClosedCommunity timed out")
    72  	case <-done:
    73  		// validate concurrent updating community result
    74  		lastCommunity, err := s.m.GetCommunityByID(communityID)
    75  		s.Require().NoError(err)
    76  		s.Require().Len(lastCommunity.Categories(), 2)
    77  		s.Require().Len(lastCommunity.Description().Chats, 4)
    78  	}
    79  }