storj.io/minio@v0.0.0-20230509071714-0cbc90f649b1/pkg/event/targetlist_test.go (about)

     1  /*
     2   * MinIO Cloud Storage, (C) 2018 MinIO, Inc.
     3   *
     4   * Licensed under the Apache License, Version 2.0 (the "License");
     5   * you may not use this file except in compliance with the License.
     6   * You may obtain a copy of the License at
     7   *
     8   *     http://www.apache.org/licenses/LICENSE-2.0
     9   *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   */
    16  
    17  package event
    18  
    19  import (
    20  	"crypto/rand"
    21  	"errors"
    22  	"reflect"
    23  	"testing"
    24  	"time"
    25  )
    26  
    27  type ExampleTarget struct {
    28  	id       TargetID
    29  	sendErr  bool
    30  	closeErr bool
    31  }
    32  
    33  func (target ExampleTarget) ID() TargetID {
    34  	return target.id
    35  }
    36  
    37  // Save - Sends event directly without persisting.
    38  func (target ExampleTarget) Save(eventData Event) error {
    39  	return target.send(eventData)
    40  }
    41  
    42  func (target ExampleTarget) send(eventData Event) error {
    43  	b := make([]byte, 1)
    44  	if _, err := rand.Read(b); err != nil {
    45  		panic(err)
    46  	}
    47  
    48  	time.Sleep(time.Duration(b[0]) * time.Millisecond)
    49  
    50  	if target.sendErr {
    51  		return errors.New("send error")
    52  	}
    53  
    54  	return nil
    55  }
    56  
    57  // Send - interface compatible method does no-op.
    58  func (target ExampleTarget) Send(eventKey string) error {
    59  	return nil
    60  }
    61  
    62  func (target ExampleTarget) Close() error {
    63  	if target.closeErr {
    64  		return errors.New("close error")
    65  	}
    66  
    67  	return nil
    68  }
    69  
    70  func (target ExampleTarget) IsActive() (bool, error) {
    71  	return false, errors.New("not connected to target server/service")
    72  }
    73  
    74  // HasQueueStore - No-Op. Added for interface compatibility
    75  func (target ExampleTarget) HasQueueStore() bool {
    76  	return false
    77  }
    78  
    79  func TestTargetListAdd(t *testing.T) {
    80  	targetListCase1 := NewTargetList()
    81  
    82  	targetListCase2 := NewTargetList()
    83  	if err := targetListCase2.Add(&ExampleTarget{TargetID{"2", "testcase"}, false, false}); err != nil {
    84  		panic(err)
    85  	}
    86  
    87  	targetListCase3 := NewTargetList()
    88  	if err := targetListCase3.Add(&ExampleTarget{TargetID{"3", "testcase"}, false, false}); err != nil {
    89  		panic(err)
    90  	}
    91  
    92  	testCases := []struct {
    93  		targetList     *TargetList
    94  		target         Target
    95  		expectedResult []TargetID
    96  		expectErr      bool
    97  	}{
    98  		{targetListCase1, &ExampleTarget{TargetID{"1", "webhook"}, false, false}, []TargetID{{"1", "webhook"}}, false},
    99  		{targetListCase2, &ExampleTarget{TargetID{"1", "webhook"}, false, false}, []TargetID{{"2", "testcase"}, {"1", "webhook"}}, false},
   100  		{targetListCase3, &ExampleTarget{TargetID{"3", "testcase"}, false, false}, nil, true},
   101  	}
   102  
   103  	for i, testCase := range testCases {
   104  		err := testCase.targetList.Add(testCase.target)
   105  		expectErr := (err != nil)
   106  
   107  		if expectErr != testCase.expectErr {
   108  			t.Fatalf("test %v: error: expected: %v, got: %v", i+1, testCase.expectErr, expectErr)
   109  		}
   110  
   111  		if !testCase.expectErr {
   112  			result := testCase.targetList.List()
   113  
   114  			if len(result) != len(testCase.expectedResult) {
   115  				t.Fatalf("test %v: data: expected: %v, got: %v", i+1, testCase.expectedResult, result)
   116  			}
   117  
   118  			for _, targetID1 := range result {
   119  				var found bool
   120  				for _, targetID2 := range testCase.expectedResult {
   121  					if reflect.DeepEqual(targetID1, targetID2) {
   122  						found = true
   123  						break
   124  					}
   125  				}
   126  				if !found {
   127  					t.Fatalf("test %v: data: expected: %v, got: %v", i+1, testCase.expectedResult, result)
   128  				}
   129  			}
   130  		}
   131  	}
   132  }
   133  
   134  func TestTargetListExists(t *testing.T) {
   135  	targetListCase1 := NewTargetList()
   136  
   137  	targetListCase2 := NewTargetList()
   138  	if err := targetListCase2.Add(&ExampleTarget{TargetID{"2", "testcase"}, false, false}); err != nil {
   139  		panic(err)
   140  	}
   141  
   142  	targetListCase3 := NewTargetList()
   143  	if err := targetListCase3.Add(&ExampleTarget{TargetID{"3", "testcase"}, false, false}); err != nil {
   144  		panic(err)
   145  	}
   146  
   147  	testCases := []struct {
   148  		targetList     *TargetList
   149  		targetID       TargetID
   150  		expectedResult bool
   151  	}{
   152  		{targetListCase1, TargetID{"1", "webhook"}, false},
   153  		{targetListCase2, TargetID{"1", "webhook"}, false},
   154  		{targetListCase3, TargetID{"3", "testcase"}, true},
   155  	}
   156  
   157  	for i, testCase := range testCases {
   158  		result := testCase.targetList.Exists(testCase.targetID)
   159  
   160  		if result != testCase.expectedResult {
   161  			t.Fatalf("test %v: data: expected: %v, got: %v", i+1, testCase.expectedResult, result)
   162  		}
   163  	}
   164  }
   165  
   166  func TestTargetListList(t *testing.T) {
   167  	targetListCase1 := NewTargetList()
   168  
   169  	targetListCase2 := NewTargetList()
   170  	if err := targetListCase2.Add(&ExampleTarget{TargetID{"2", "testcase"}, false, false}); err != nil {
   171  		panic(err)
   172  	}
   173  
   174  	targetListCase3 := NewTargetList()
   175  	if err := targetListCase3.Add(&ExampleTarget{TargetID{"3", "testcase"}, false, false}); err != nil {
   176  		panic(err)
   177  	}
   178  	if err := targetListCase3.Add(&ExampleTarget{TargetID{"1", "webhook"}, false, false}); err != nil {
   179  		panic(err)
   180  	}
   181  
   182  	testCases := []struct {
   183  		targetList     *TargetList
   184  		expectedResult []TargetID
   185  	}{
   186  		{targetListCase1, []TargetID{}},
   187  		{targetListCase2, []TargetID{{"2", "testcase"}}},
   188  		{targetListCase3, []TargetID{{"3", "testcase"}, {"1", "webhook"}}},
   189  	}
   190  
   191  	for i, testCase := range testCases {
   192  		result := testCase.targetList.List()
   193  
   194  		if len(result) != len(testCase.expectedResult) {
   195  			t.Fatalf("test %v: data: expected: %v, got: %v", i+1, testCase.expectedResult, result)
   196  		}
   197  
   198  		for _, targetID1 := range result {
   199  			var found bool
   200  			for _, targetID2 := range testCase.expectedResult {
   201  				if reflect.DeepEqual(targetID1, targetID2) {
   202  					found = true
   203  					break
   204  				}
   205  			}
   206  			if !found {
   207  				t.Fatalf("test %v: data: expected: %v, got: %v", i+1, testCase.expectedResult, result)
   208  			}
   209  		}
   210  	}
   211  }
   212  
   213  func TestTargetListSend(t *testing.T) {
   214  	targetListCase1 := NewTargetList()
   215  
   216  	targetListCase2 := NewTargetList()
   217  	if err := targetListCase2.Add(&ExampleTarget{TargetID{"2", "testcase"}, false, false}); err != nil {
   218  		panic(err)
   219  	}
   220  
   221  	targetListCase3 := NewTargetList()
   222  	if err := targetListCase3.Add(&ExampleTarget{TargetID{"3", "testcase"}, false, false}); err != nil {
   223  		panic(err)
   224  	}
   225  
   226  	targetListCase4 := NewTargetList()
   227  	if err := targetListCase4.Add(&ExampleTarget{TargetID{"4", "testcase"}, true, false}); err != nil {
   228  		panic(err)
   229  	}
   230  
   231  	testCases := []struct {
   232  		targetList *TargetList
   233  		targetID   TargetID
   234  		expectErr  bool
   235  	}{
   236  		{targetListCase1, TargetID{"1", "webhook"}, false},
   237  		{targetListCase2, TargetID{"1", "non-existent"}, false},
   238  		{targetListCase3, TargetID{"3", "testcase"}, false},
   239  		{targetListCase4, TargetID{"4", "testcase"}, true},
   240  	}
   241  
   242  	resCh := make(chan TargetIDResult)
   243  	for i, testCase := range testCases {
   244  		testCase.targetList.Send(Event{}, map[TargetID]struct{}{
   245  			testCase.targetID: {},
   246  		}, resCh)
   247  		res := <-resCh
   248  		expectErr := (res.Err != nil)
   249  
   250  		if expectErr != testCase.expectErr {
   251  			t.Fatalf("test %v: error: expected: %v, got: %v", i+1, testCase.expectErr, expectErr)
   252  		}
   253  	}
   254  }
   255  
   256  func TestNewTargetList(t *testing.T) {
   257  	if result := NewTargetList(); result == nil {
   258  		t.Fatalf("test: result: expected: <non-nil>, got: <nil>")
   259  	}
   260  }