github.com/google/fleetspeak@v0.1.15-0.20240426164851-4f31f62c1aea/fleetspeak/src/server/service/service_test.go (about)

     1  // Copyright 2017 Google Inc.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     https://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package service
    16  
    17  import (
    18  	"errors"
    19  	"testing"
    20  )
    21  
    22  type netErr struct {
    23  	msg        string
    24  	temp, time bool
    25  }
    26  
    27  func (e netErr) Error() string {
    28  	return e.msg
    29  }
    30  
    31  func (e netErr) Temporary() bool {
    32  	return e.temp
    33  }
    34  
    35  func (e netErr) Timeout() bool {
    36  	return e.time
    37  }
    38  
    39  func TestIsTemporary(t *testing.T) {
    40  	for _, tc := range []struct {
    41  		e    error
    42  		want bool
    43  	}{
    44  		{
    45  			e:    errors.New("a permanent error"),
    46  			want: false,
    47  		},
    48  		{
    49  			e:    TemporaryError{errors.New("a temporary error")},
    50  			want: true,
    51  		},
    52  		{
    53  			e:    &TemporaryError{errors.New("a referenced temporary error")},
    54  			want: true,
    55  		},
    56  		{
    57  			e:    netErr{msg: "A generic net.Error"},
    58  			want: false,
    59  		},
    60  		{
    61  			e:    netErr{msg: "A temporary net.Error", temp: true},
    62  			want: true,
    63  		},
    64  		{
    65  			e:    netErr{msg: "A timeout net.Error", time: true},
    66  			want: true,
    67  		},
    68  	} {
    69  		got := IsTemporary(tc.e)
    70  		if got != tc.want {
    71  			t.Errorf("IsTemporary(%T)=%v, but want %v", tc.e, got, tc.want)
    72  		}
    73  	}
    74  }