github.com/projecteru2/core@v0.0.0-20240321043226-06bcc1c23f58/store/redis/service_test.go (about)

     1  package redis
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"sort"
     7  	"time"
     8  )
     9  
    10  func (s *RediaronTestSuite) TestRegisterServiceWithDeregister() {
    11  	m := s.rediaron
    12  	ctx := context.Background()
    13  	svc := "svc"
    14  	path := fmt.Sprintf(serviceStatusKey, svc)
    15  	_, deregister, err := m.RegisterService(ctx, svc, time.Minute)
    16  	s.NoError(err)
    17  
    18  	v, err := m.GetOne(ctx, path)
    19  	s.NoError(err)
    20  	s.Equal(ephemeralValue, v)
    21  
    22  	deregister()
    23  	//time.Sleep(time.Second)
    24  	v, err = m.GetOne(ctx, path)
    25  	s.Error(err)
    26  	s.Empty(v)
    27  }
    28  
    29  func (s *RediaronTestSuite) TestServiceStatusStream() {
    30  	m := s.rediaron
    31  	ctx, cancel := context.WithCancel(context.Background())
    32  
    33  	go func() {
    34  		time.Sleep(3 * time.Second)
    35  		cancel()
    36  	}()
    37  
    38  	_, unregisterService1, err := m.RegisterService(ctx, "127.0.0.1:5001", time.Second)
    39  	s.NoError(err)
    40  
    41  	ch, err := m.ServiceStatusStream(ctx)
    42  	s.NoError(err)
    43  
    44  	s.Equal(<-ch, []string{"127.0.0.1:5001"})
    45  
    46  	// register and triggers set manually
    47  	_, _, err = m.RegisterService(ctx, "127.0.0.1:5002", time.Second)
    48  	s.NoError(err)
    49  	time.Sleep(500 * time.Millisecond)
    50  	triggerMockedKeyspaceNotification(s.rediaron.cli, fmt.Sprintf(serviceStatusKey, "127.0.0.1:5002"), actionSet)
    51  
    52  	endpoints := <-ch
    53  	sort.Strings(endpoints)
    54  	s.Equal(endpoints, []string{"127.0.0.1:5001", "127.0.0.1:5002"})
    55  
    56  	// unregister and triggers del manually
    57  	_, _, err = m.RegisterService(ctx, "127.0.0.1:5002", time.Second)
    58  	unregisterService1()
    59  	time.Sleep(500 * time.Millisecond)
    60  	triggerMockedKeyspaceNotification(s.rediaron.cli, fmt.Sprintf(serviceStatusKey, "127.0.0.1:5001"), actionDel)
    61  
    62  	// trigger fastforward to clean the value
    63  	s.rediserver.FastForward(time.Second)
    64  	s.Equal(<-ch, []string{"127.0.0.1:5002"})
    65  }