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

     1  package cache
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/Axway/agent-sdk/pkg/notification"
     7  
     8  	"github.com/stretchr/testify/assert"
     9  )
    10  
    11  func TestPubSub(t *testing.T) {
    12  	// CreateTopic
    13  	topic1 := "topic1"
    14  	data1 := "topic1 data1"
    15  	_, err := CreateTopic("niltopic")
    16  	assert.Nil(t, err, "Unexpected error hit creating a topic with nil as the initial data")
    17  	createpubsub, err := CreateTopicWithInitData(topic1, data1)
    18  	assert.Nil(t, err, "Unexpected error hit in Create Topic")
    19  	assert.IsType(t, &cachePubSub{}, createpubsub, "Returned object not of cachePubSub type")
    20  	pubsub, err := CreateTopicWithInitData(topic1, data1)
    21  	assert.NotNil(t, err, "Expected a duplicate topic error")
    22  	assert.Nil(t, pubsub, "The returned PubSub object should have been nil")
    23  	tempName := "tempName"
    24  	notification.RegisterNotifier(tempName, nil)
    25  	pubsub, err = CreateTopicWithInitData(tempName, data1)
    26  	assert.NotNil(t, err, "Expected a duplicate topic error")
    27  	assert.Nil(t, pubsub, "The returned PubSub object should have been nil")
    28  
    29  	// RemoveTopic
    30  	removetopic := "removetopic"
    31  	assert.Len(t, topics, 2, "The length of topics was not what was expected")
    32  	_, err = CreateTopic(removetopic)
    33  	assert.Len(t, topics, 3, "Expected a new topic in the topics array")
    34  	assert.Nil(t, err, "Unexpected error hit creating a topic with nil as the initial data")
    35  	err = RemoveTopic(removetopic)
    36  	assert.Len(t, topics, 2, "Expected the topics array to be 1 less")
    37  	assert.Nil(t, err, "Unexpected error hit removing a topic")
    38  	err = RemoveTopic("badtopicname")
    39  	assert.Len(t, topics, 2, "Expected the topics array length to not have changed")
    40  	assert.NotNil(t, err, "Expected an error to be returned from a bad topic name")
    41  	CreateTopic(removetopic)
    42  	assert.Len(t, topics, 3, "Expected the topics array length to have grown")
    43  	globalCache.Delete(removetopic)
    44  	err = RemoveTopic(removetopic)
    45  	assert.Len(t, topics, 2, "Expected the topics array length to have been 1 less")
    46  	assert.NotNil(t, err, "Expected an error to be returned when removing a topic without a cache item")
    47  
    48  	// GetPubSub
    49  	getpubsub, err := GetPubSub(topic1)
    50  	assert.Nil(t, err, "Unexpected error hit in Create Topic")
    51  	assert.IsType(t, &cachePubSub{}, getpubsub, "Returned object not of cachePubSub type")
    52  	assert.Equal(t, createpubsub, getpubsub, "Expected the PubSub object to be the same as the one previously created")
    53  	pubsub, err = GetPubSub(tempName)
    54  	assert.NotNil(t, err, "Expected a could not find topic error")
    55  	assert.Nil(t, pubsub, "The returned PubSub object should have been nil")
    56  
    57  	// Publish and Subscribe
    58  	topic2 := "topic2"
    59  	data2 := "topic2 data1"
    60  	pubsub2, err := CreateTopicWithInitData(topic2, data2)
    61  	assert.Nil(t, err, "Unexpected error hit in Create Topic")
    62  	assert.NotNil(t, pubsub2, "Unexpected nil for pubsub object")
    63  	subChan, id := pubsub2.Subscribe()
    64  	assert.NotNil(t, id, "Expected an ID to be returned from Subscribe")
    65  
    66  	dataReceived := ""
    67  	dataChan := make(chan struct{})
    68  	go func() {
    69  		for {
    70  			data, ok := <-subChan
    71  			if !ok {
    72  				return
    73  			}
    74  			dataReceived = data.(string)
    75  			dataChan <- struct{}{}
    76  		}
    77  	}()
    78  
    79  	err = pubsub2.Publish("topic2", "", map[string]interface{}{"foo": make(chan int)})
    80  	assert.NotNil(t, err, "Expected error since data can't be marshaled")
    81  	err = pubsub2.Publish("topic2", "", data2)
    82  	assert.Nil(t, err, "Unexpected error hit in Publish")
    83  	assert.Equal(t, "", dataReceived, "Data changed unexpectedly")
    84  	data2a := "topic2 data2"
    85  	err = pubsub2.Publish("topic2", "", data2a)
    86  	<-dataChan // Wait for the go function to have been executed
    87  	assert.Nil(t, err, "Unexpected error hit in Publish")
    88  	assert.Equal(t, data2a, dataReceived, "Data changed successfully")
    89  
    90  	// PublishToTopic
    91  	data2b := "topic2 data2b"
    92  	err = pubsub2.PublishToTopic(data2b)
    93  	<-dataChan // Wait for the go function to have been executed
    94  	assert.Nil(t, err, "Unexpected error hit in Publish")
    95  	assert.Equal(t, data2b, dataReceived, "Data changed successfully")
    96  
    97  	// PublishToTopicWithSecondaryKey
    98  	data2c := "topic2 data2c"
    99  	err = pubsub2.PublishToTopicWithSecondaryKey("", data2c)
   100  	<-dataChan // Wait for the go function to have been executed
   101  	assert.Nil(t, err, "Unexpected error hit in Publish")
   102  	assert.Equal(t, data2c, dataReceived, "Data changed successfully")
   103  
   104  	// PublishCacheHash
   105  	data2d := "topic2 data2d"
   106  	err = pubsub2.PublishCacheHash("topic2", "", data2d)
   107  	<-dataChan // Wait for the go function to have been executed
   108  	assert.Nil(t, err, "Unexpected error hit in Publish")
   109  	assert.Equal(t, data2d, dataReceived, "Data changed successfully")
   110  
   111  	// PublishCacheHashToTopic
   112  	data2e := "topic2 data2e"
   113  	err = pubsub2.PublishCacheHashToTopic(data2e)
   114  	<-dataChan // Wait for the go function to have been executed
   115  	assert.Nil(t, err, "Unexpected error hit in Publish")
   116  	assert.Equal(t, data2e, dataReceived, "Data changed successfully")
   117  
   118  	// PublishCacheHashToTopicWithSecondaryKey
   119  	data2f := "topic2 data2f"
   120  	err = pubsub2.PublishCacheHashToTopicWithSecondaryKey("", data2f)
   121  	<-dataChan // Wait for the go function to have been executed
   122  	assert.Nil(t, err, "Unexpected error hit in Publish")
   123  	assert.Equal(t, data2f, dataReceived, "Data changed successfully")
   124  
   125  	// Publish and SubscribeWithCallback
   126  	topic3 := "topic3"
   127  	data3 := "topic3 data1"
   128  	pubsub3, _ := CreateTopicWithInitData(topic3, data3)
   129  
   130  	dataReceived = ""
   131  	cbCalled := make(chan struct{})
   132  	cbFunc := func(data interface{}) {
   133  		dataReceived = data.(string)
   134  		close(cbCalled)
   135  	}
   136  	subID := pubsub3.SubscribeWithCallback(cbFunc)
   137  	assert.NotNil(t, subID, "Expected an ID to be returned from Subscribe")
   138  
   139  	err = pubsub3.Publish("topic3", "", map[string]interface{}{"foo": make(chan int)})
   140  	assert.NotNil(t, err, "Expected error since data can't be marshaled")
   141  	err = pubsub3.Publish("topic3", "", data3)
   142  	assert.Nil(t, err, "Unexpected error hit in Publish")
   143  	assert.Equal(t, "", dataReceived, "Data changed unexpectedly")
   144  	data3a := "topic3 data3"
   145  	err = pubsub3.Publish("topic3", "", data3a)
   146  	<-cbCalled // Wait for the callback function to have been executed
   147  	assert.Nil(t, err, "Unexpected error hit in Publish")
   148  	assert.Equal(t, data3a, dataReceived, "Data changed successfully")
   149  
   150  	// Unsubscribe
   151  	err = pubsub2.Unsubscribe(id)
   152  	assert.Nil(t, err, "Unexpected error hit in Unsubscribe")
   153  	err = pubsub3.Unsubscribe(subID)
   154  	assert.Nil(t, err, "Unexpected error hit in Unsubscribe")
   155  }