golang.org/x/build@v0.0.0-20240506185731-218518f32b70/cmd/coordinator/internal/dashboard/datastore_test.go (about)

     1  // Copyright 2020 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  //go:build linux || darwin
     6  
     7  package dashboard
     8  
     9  import (
    10  	"errors"
    11  	"testing"
    12  
    13  	"cloud.google.com/go/datastore"
    14  )
    15  
    16  var testError = errors.New("this is a test error for cmd/coordinator")
    17  
    18  func ignoreTestError(err error) error {
    19  	if !errors.Is(err, testError) {
    20  		return err
    21  	}
    22  	return nil
    23  }
    24  
    25  func ignoreNothing(err error) error {
    26  	return err
    27  }
    28  
    29  func TestFilterMultiError(t *testing.T) {
    30  	cases := []struct {
    31  		desc    string
    32  		err     error
    33  		ignores []ignoreFunc
    34  		wantErr bool
    35  	}{
    36  		{
    37  			desc:    "single ignored error",
    38  			err:     datastore.MultiError{testError},
    39  			ignores: []ignoreFunc{ignoreTestError},
    40  		},
    41  		{
    42  			desc:    "multiple ignored errors",
    43  			err:     datastore.MultiError{testError, testError},
    44  			ignores: []ignoreFunc{ignoreTestError},
    45  		},
    46  		{
    47  			desc:    "non-ignored error",
    48  			err:     datastore.MultiError{testError, errors.New("this should fail")},
    49  			ignores: []ignoreFunc{ignoreTestError},
    50  			wantErr: true,
    51  		},
    52  		{
    53  			desc:    "nil error",
    54  			ignores: []ignoreFunc{ignoreTestError},
    55  		},
    56  		{
    57  			desc:    "non-multistore error",
    58  			err:     errors.New("this should fail"),
    59  			ignores: []ignoreFunc{ignoreTestError},
    60  			wantErr: true,
    61  		},
    62  		{
    63  			desc:    "no ignoreFuncs",
    64  			err:     errors.New("this should fail"),
    65  			ignores: []ignoreFunc{},
    66  			wantErr: true,
    67  		},
    68  		{
    69  			desc:    "if any ignoreFunc ignores, error is ignored.",
    70  			err:     datastore.MultiError{testError, testError},
    71  			ignores: []ignoreFunc{ignoreNothing, ignoreTestError},
    72  		},
    73  	}
    74  	for _, c := range cases {
    75  		t.Run(c.desc, func(t *testing.T) {
    76  			if err := filterMultiError(c.err, c.ignores...); (err != nil) != c.wantErr {
    77  				t.Errorf("filterMultiError(%v, %v) = %v, wantErr = %v", c.err, c.ignores, err, c.wantErr)
    78  			}
    79  		})
    80  	}
    81  }
    82  
    83  func TestIgnoreNoSuchEntity(t *testing.T) {
    84  	if err := ignoreNoSuchEntity(datastore.ErrNoSuchEntity); err != nil {
    85  		t.Errorf("ignoreNoSuchEntity(%v) = %v, wanted no error", datastore.ErrNoSuchEntity, err)
    86  	}
    87  	if err := ignoreNoSuchEntity(datastore.ErrInvalidKey); err == nil {
    88  		t.Errorf("ignoreNoSuchEntity(%v) = %v, wanted error", datastore.ErrInvalidKey, err)
    89  	}
    90  }