github.com/Axway/agent-sdk@v1.1.101/pkg/agent/poller/poller_test.go (about)

     1  package poller
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  
     7  	agentcache "github.com/Axway/agent-sdk/pkg/agent/cache"
     8  	"github.com/Axway/agent-sdk/pkg/agent/events"
     9  	management "github.com/Axway/agent-sdk/pkg/apic/apiserver/models/management/v1alpha1"
    10  	"github.com/Axway/agent-sdk/pkg/apic/mock"
    11  	"github.com/Axway/agent-sdk/pkg/config"
    12  	"github.com/Axway/agent-sdk/pkg/watchmanager/proto"
    13  	"github.com/stretchr/testify/assert"
    14  )
    15  
    16  func TestPollerRegisterWatch(t *testing.T) {
    17  	cacheManager := agentcache.NewAgentCacheManager(cfg, false)
    18  	wt := management.NewWatchTopic("mocktopic")
    19  	seq := events.NewSequenceProvider(cacheManager, wt.Name)
    20  	seq.SetSequence(1)
    21  	mockH := &mockHarvester{}
    22  
    23  	poller := newPollExecutor(cfg.PollInterval, withHarvester(harvesterConfig{
    24  		sequence:      seq,
    25  		topicSelfLink: wt.GetSelfLink(),
    26  		hClient:       mockH,
    27  	}))
    28  
    29  	eventCh, errCh := make(chan *proto.Event), make(chan error)
    30  	h := &mockHarvester{
    31  		eventCh: eventCh,
    32  	}
    33  
    34  	poller.harvester = h
    35  	poller.RegisterWatch(eventCh, errCh)
    36  
    37  	evt := <-h.eventCh
    38  	assert.NotNil(t, evt)
    39  }
    40  
    41  func TestPollerRegisterWatchError(t *testing.T) {
    42  	cacheManager := agentcache.NewAgentCacheManager(cfg, false)
    43  	wt := management.NewWatchTopic("mocktopic")
    44  	seq := events.NewSequenceProvider(cacheManager, wt.Name)
    45  	mockH := &mockHarvester{}
    46  
    47  	poller := newPollExecutor(cfg.PollInterval, withHarvester(harvesterConfig{
    48  		sequence:      seq,
    49  		topicSelfLink: wt.GetSelfLink(),
    50  		hClient:       mockH,
    51  	}))
    52  
    53  	eventCh, errCh := make(chan *proto.Event), make(chan error)
    54  	poller.harvester = &mockHarvester{
    55  		err: fmt.Errorf("harvester error"),
    56  	}
    57  
    58  	poller.RegisterWatch(eventCh, errCh)
    59  
    60  	err := <-errCh
    61  	assert.NotNil(t, err)
    62  }
    63  
    64  func TestPollClientOptions(t *testing.T) {
    65  	cfg := config.NewCentralConfig(config.DiscoveryAgent)
    66  	pc, _ := NewPollClient(
    67  		&mock.Client{}, cfg, nil,
    68  		WithHarvester(&mockHarvester{}, &mockSequence{}, "/self/link"),
    69  		WithOnClientStop(func() {}),
    70  		WithOnConnect(),
    71  	)
    72  
    73  	assert.NotNil(t, pc.harvesterConfig.hClient)
    74  	assert.NotNil(t, pc.harvesterConfig.sequence)
    75  	assert.NotNil(t, pc.harvesterConfig.topicSelfLink)
    76  	assert.NotNil(t, pc.onClientStop)
    77  	assert.NotNil(t, pc.onStreamConnection)
    78  }
    79  
    80  type mockSequence struct{}
    81  
    82  func (m mockSequence) GetSequence() int64 {
    83  	return 0
    84  }
    85  
    86  func (m mockSequence) SetSequence(_ int64) {
    87  
    88  }