github.com/openebs/node-disk-manager@v1.9.1-0.20230225014141-4531f06ffa1e/pkg/epoll/epoll_test.go (about)

     1  /*
     2  Copyright 2020 The OpenEBS Authors
     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 epoll
    18  
    19  import (
    20  	"path/filepath"
    21  	"testing"
    22  
    23  	"github.com/stretchr/testify/assert"
    24  )
    25  
    26  func TestNew(t *testing.T) {
    27  	ep, err := New()
    28  	if err != nil {
    29  		assert.Equal(t, Epoll{}, ep)
    30  	}
    31  	ep.Close()
    32  }
    33  
    34  func TestStart(t *testing.T) {
    35  	_, err := New()
    36  	if err != nil {
    37  		t.Fatal("failed to create epoll instance: ", err)
    38  	}
    39  }
    40  
    41  func TestAddWatcher(t *testing.T) {
    42  	ep, err := New()
    43  	if err != nil {
    44  		t.Fatal("failed to create epoll instance: ", err)
    45  	}
    46  	t.Cleanup(ep.Close)
    47  	testfileName := "/proc/self/mounts"
    48  	eventTypes := []EventType{EPOLLERR, EPOLLIN}
    49  	// Note: order of test cases matters.
    50  	testCases := []struct {
    51  		name        string
    52  		watcher     Watcher
    53  		expectErr   bool
    54  		expectedErr error
    55  	}{
    56  		{
    57  			name: "add a new valid watcher",
    58  			watcher: Watcher{
    59  				FileName:   testfileName,
    60  				EventTypes: eventTypes,
    61  			},
    62  			expectErr:   false,
    63  			expectedErr: nil,
    64  		},
    65  		{
    66  			name: "invalid event type",
    67  			watcher: Watcher{
    68  				FileName:   testfileName,
    69  				EventTypes: []EventType{EPOLLERR, 10},
    70  			},
    71  			expectErr:   true,
    72  			expectedErr: ErrInvalidEventType,
    73  		},
    74  		{
    75  			name: "file already being watched",
    76  			watcher: Watcher{
    77  				FileName:   testfileName,
    78  				EventTypes: eventTypes,
    79  			},
    80  			expectErr:   true,
    81  			expectedErr: ErrFileAlreadyWatched,
    82  		},
    83  		{
    84  			name: "file not present",
    85  			watcher: Watcher{
    86  				FileName:   filepath.Join(t.TempDir(), "non-existent"),
    87  				EventTypes: eventTypes,
    88  			},
    89  			expectErr:   true,
    90  			expectedErr: nil,
    91  		},
    92  	}
    93  
    94  	for _, tc := range testCases {
    95  		t.Run(tc.name, func(t *testing.T) {
    96  			err := ep.AddWatcher(tc.watcher)
    97  			if tc.expectErr {
    98  				assert.NotNil(t, err)
    99  				if tc.expectedErr != nil {
   100  					assert.Equal(t, tc.expectedErr, err)
   101  				}
   102  			} else {
   103  				assert.Nil(t, err)
   104  			}
   105  		})
   106  	}
   107  
   108  }
   109  
   110  func TestDeleteWatcher(t *testing.T) {
   111  	ep, err := New()
   112  	if err != nil {
   113  		t.Fatal("failed to create epoll instance: ", err)
   114  	}
   115  	t.Cleanup(ep.Close)
   116  	fileName := "/proc/self/mounts"
   117  	watcher := Watcher{
   118  		FileName:   fileName,
   119  		EventTypes: []EventType{EPOLLIN},
   120  	}
   121  
   122  	t.Run("watcher not present", func(t *testing.T) {
   123  		if ep.DeleteWatcher(fileName) != ErrWatcherNotFound {
   124  			t.Fail()
   125  		}
   126  	})
   127  
   128  	if ep.AddWatcher(watcher) != nil {
   129  		t.Fatal("failed to add watcher")
   130  	}
   131  
   132  	t.Run("delete watcher", func(t *testing.T) {
   133  		if ep.DeleteWatcher(fileName) != nil {
   134  			t.Fail()
   135  		}
   136  	})
   137  }