github.com/Ilhicas/nomad@v1.0.4-0.20210304152020-e86851182bc3/nomad/stream/subscription_test.go (about)

     1  package stream
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/hashicorp/nomad/nomad/structs"
     7  
     8  	"github.com/stretchr/testify/require"
     9  )
    10  
    11  func TestFilter_AllTopics(t *testing.T) {
    12  	events := make([]structs.Event, 0, 5)
    13  	events = append(events, structs.Event{Topic: "Test", Key: "One"}, structs.Event{Topic: "Test", Key: "Two"})
    14  
    15  	req := &SubscribeRequest{
    16  		Topics: map[structs.Topic][]string{
    17  			"*": {"*"},
    18  		},
    19  	}
    20  	actual := filter(req, events)
    21  	require.Equal(t, events, actual)
    22  }
    23  
    24  func TestFilter_AllKeys(t *testing.T) {
    25  	events := make([]structs.Event, 0, 5)
    26  	events = append(events, structs.Event{Topic: "Test", Key: "One"}, structs.Event{Topic: "Test", Key: "Two"})
    27  
    28  	req := &SubscribeRequest{
    29  		Topics: map[structs.Topic][]string{
    30  			"Test": {"*"},
    31  		},
    32  	}
    33  	actual := filter(req, events)
    34  	require.Equal(t, events, actual)
    35  }
    36  
    37  func TestFilter_PartialMatch_Topic(t *testing.T) {
    38  	events := make([]structs.Event, 0, 5)
    39  	events = append(events, structs.Event{Topic: "Test", Key: "One"}, structs.Event{Topic: "Test", Key: "Two"}, structs.Event{Topic: "Exclude", Key: "Two"})
    40  
    41  	req := &SubscribeRequest{
    42  		Topics: map[structs.Topic][]string{
    43  			"Test": {"*"},
    44  		},
    45  	}
    46  	actual := filter(req, events)
    47  	expected := []structs.Event{{Topic: "Test", Key: "One"}, {Topic: "Test", Key: "Two"}}
    48  	require.Equal(t, expected, actual)
    49  
    50  	require.Equal(t, 2, cap(actual))
    51  }
    52  
    53  func TestFilter_Match_TopicAll_SpecificKey(t *testing.T) {
    54  	events := []structs.Event{
    55  		{Topic: "Match", Key: "Two"},
    56  		{Topic: "NoMatch", Key: "One"},
    57  		{Topic: "OtherMatch", Key: "Two"},
    58  	}
    59  
    60  	req := &SubscribeRequest{
    61  		Topics: map[structs.Topic][]string{
    62  			"*": {"Two"},
    63  		},
    64  	}
    65  
    66  	actual := filter(req, events)
    67  	expected := []structs.Event{
    68  		{Topic: "Match", Key: "Two"},
    69  		{Topic: "OtherMatch", Key: "Two"},
    70  	}
    71  	require.Equal(t, expected, actual)
    72  }
    73  
    74  func TestFilter_Match_TopicAll_SpecificKey_Plus(t *testing.T) {
    75  	events := []structs.Event{
    76  		{Topic: "FirstTwo", Key: "Two"},
    77  		{Topic: "Test", Key: "One"},
    78  		{Topic: "SecondTwo", Key: "Two"},
    79  	}
    80  
    81  	req := &SubscribeRequest{
    82  		Topics: map[structs.Topic][]string{
    83  			"*":    {"Two"},
    84  			"Test": {"One"},
    85  		},
    86  	}
    87  
    88  	actual := filter(req, events)
    89  	expected := []structs.Event{
    90  		{Topic: "FirstTwo", Key: "Two"},
    91  		{Topic: "Test", Key: "One"},
    92  		{Topic: "SecondTwo", Key: "Two"},
    93  	}
    94  	require.Equal(t, expected, actual)
    95  }
    96  
    97  func TestFilter_PartialMatch_Key(t *testing.T) {
    98  	events := make([]structs.Event, 0, 5)
    99  	events = append(events, structs.Event{Topic: "Test", Key: "One"}, structs.Event{Topic: "Test", Key: "Two"})
   100  
   101  	req := &SubscribeRequest{
   102  		Topics: map[structs.Topic][]string{
   103  			"Test": {"One"},
   104  		},
   105  	}
   106  	actual := filter(req, events)
   107  	expected := []structs.Event{{Topic: "Test", Key: "One"}}
   108  	require.Equal(t, expected, actual)
   109  
   110  	require.Equal(t, 1, cap(actual))
   111  }
   112  
   113  func TestFilter_NoMatch(t *testing.T) {
   114  	events := make([]structs.Event, 0, 5)
   115  	events = append(events, structs.Event{Topic: "Test", Key: "One"}, structs.Event{Topic: "Test", Key: "Two"})
   116  
   117  	req := &SubscribeRequest{
   118  		Topics: map[structs.Topic][]string{
   119  			"NodeEvents": {"*"},
   120  			"Test":       {"Highly-Specific-Key"},
   121  		},
   122  	}
   123  	actual := filter(req, events)
   124  	var expected []structs.Event
   125  	require.Equal(t, expected, actual)
   126  
   127  	require.Equal(t, 0, cap(actual))
   128  }
   129  
   130  func TestFilter_Namespace(t *testing.T) {
   131  	events := make([]structs.Event, 0, 5)
   132  	events = append(events, structs.Event{Topic: "Test", Key: "One", Namespace: "foo"}, structs.Event{Topic: "Test", Key: "Two"}, structs.Event{Topic: "Test", Key: "Two", Namespace: "bar"})
   133  
   134  	req := &SubscribeRequest{
   135  		Topics: map[structs.Topic][]string{
   136  			"*": {"*"},
   137  		},
   138  		Namespace: "foo",
   139  	}
   140  	actual := filter(req, events)
   141  	expected := []structs.Event{
   142  		{Topic: "Test", Key: "One", Namespace: "foo"},
   143  		{Topic: "Test", Key: "Two"},
   144  	}
   145  	require.Equal(t, expected, actual)
   146  
   147  	require.Equal(t, 2, cap(actual))
   148  }
   149  
   150  func TestFilter_FilterKeys(t *testing.T) {
   151  	events := make([]structs.Event, 0, 5)
   152  	events = append(events, structs.Event{Topic: "Test", Key: "One", FilterKeys: []string{"extra-key"}}, structs.Event{Topic: "Test", Key: "Two"}, structs.Event{Topic: "Test", Key: "Two"})
   153  
   154  	req := &SubscribeRequest{
   155  		Topics: map[structs.Topic][]string{
   156  			"Test": {"extra-key"},
   157  		},
   158  		Namespace: "foo",
   159  	}
   160  	actual := filter(req, events)
   161  	expected := []structs.Event{
   162  		{Topic: "Test", Key: "One", FilterKeys: []string{"extra-key"}},
   163  	}
   164  	require.Equal(t, expected, actual)
   165  
   166  	require.Equal(t, 1, cap(actual))
   167  }