github.com/tickoalcantara12/micro/v3@v3.0.0-20221007104245-9d75b9bcbab9/util/selector/tests.go (about)

     1  // Licensed under the Apache License, Version 2.0 (the "License");
     2  // you may not use this file except in compliance with the License.
     3  // You may obtain a copy of the License at
     4  //
     5  //     https://www.apache.org/licenses/LICENSE-2.0
     6  //
     7  // Unless required by applicable law or agreed to in writing, software
     8  // distributed under the License is distributed on an "AS IS" BASIS,
     9  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    10  // See the License for the specific language governing permissions and
    11  // limitations under the License.
    12  //
    13  // Original source: github.com/micro/go-micro/v3/selector/tests.go
    14  
    15  package selector
    16  
    17  import (
    18  	"testing"
    19  
    20  	"github.com/stretchr/testify/assert"
    21  )
    22  
    23  // Tests runs all the tests against a selector to ensure the implementations are consistent
    24  func Tests(t *testing.T, s Selector) {
    25  	r1 := "127.0.0.1:8000"
    26  	r2 := "127.0.0.1:8001"
    27  
    28  	t.Run("Select", func(t *testing.T) {
    29  		t.Run("NoRoutes", func(t *testing.T) {
    30  			_, err := s.Select([]string{})
    31  			assert.Equal(t, ErrNoneAvailable, err, "Expected error to be none available")
    32  		})
    33  
    34  		t.Run("OneRoute", func(t *testing.T) {
    35  			next, err := s.Select([]string{r1})
    36  			srv := next()
    37  			assert.Nil(t, err, "Error should be nil")
    38  			assert.Equal(t, r1, srv, "Expected the route to be returned")
    39  		})
    40  
    41  		t.Run("MultipleRoutes", func(t *testing.T) {
    42  			next, err := s.Select([]string{r1, r2})
    43  			assert.Nil(t, err, "Error should be nil")
    44  			srv := next()
    45  			if srv != r1 && srv != r2 {
    46  				t.Errorf("Expected the route to be one of the inputs")
    47  			}
    48  		})
    49  	})
    50  
    51  	t.Run("Record", func(t *testing.T) {
    52  		err := s.Record(r1, nil)
    53  		assert.Nil(t, err, "Expected the error to be nil")
    54  	})
    55  
    56  	t.Run("String", func(t *testing.T) {
    57  		assert.NotEmpty(t, s.String(), "String returned a blank string")
    58  	})
    59  }