github.com/bazelbuild/rules_webtesting@v0.2.0/go/healthreporter/health_reporter_test.go (about)

     1  // Copyright 2016 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  //      http://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 healthreporter
    16  
    17  import (
    18  	"context"
    19  	"strings"
    20  	"testing"
    21  	"time"
    22  
    23  	"github.com/bazelbuild/rules_webtesting/go/errors"
    24  )
    25  
    26  func TestWaitForHealthyTimeout(t *testing.T) {
    27  	failing := &fakeHealthReporter{errors.New("fake", "error")}
    28  
    29  	ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
    30  	defer cancel()
    31  	err := WaitForHealthy(ctx, failing)
    32  	if err == nil {
    33  		t.Fatal("expected err to be non-nil")
    34  	}
    35  	if !strings.Contains(err.Error(), ctx.Err().Error()) {
    36  		t.Errorf(`expected err to contain "%s", but got %s`, ctx.Err().Error(), err.Error())
    37  	}
    38  	if !strings.Contains(err.Error(), failing.health.Error()) {
    39  		t.Errorf(`expected err to contain "%s", but got %s`, failing.health.Error(), err.Error())
    40  	}
    41  	if errors.Component(err) != failing.Name() {
    42  		t.Errorf(`expected err.Component to be "%s", but got %s`, failing.Name(), errors.Component(err))
    43  	}
    44  }
    45  
    46  func TestWaitForHealthyCancelled(t *testing.T) {
    47  	failing := &fakeHealthReporter{errors.New("fake", "error")}
    48  
    49  	ctx, cancel := context.WithCancel(context.Background())
    50  	cancel()
    51  	err := WaitForHealthy(ctx, failing)
    52  	if err == nil {
    53  		t.Fatal("expected err to be non-nil")
    54  	}
    55  	if !strings.Contains(err.Error(), ctx.Err().Error()) {
    56  		t.Errorf(`expected err to contain "%s", but got %s`, ctx.Err().Error(), err.Error())
    57  	}
    58  	if errors.Component(err) != failing.Name() {
    59  		t.Errorf(`expected err.Component to be "%s", but got %s`, failing.Name(), errors.Component(err))
    60  	}
    61  }
    62  
    63  func TestWaitForHealthySuccess(t *testing.T) {
    64  	successful := &fakeHealthReporter{}
    65  
    66  	err := WaitForHealthy(context.Background(), successful)
    67  	if err != nil {
    68  		t.Fatalf("expected err to be nil, but got %v", err)
    69  	}
    70  }
    71  
    72  type fakeHealthReporter struct {
    73  	health error
    74  }
    75  
    76  func (f *fakeHealthReporter) Name() string {
    77  	return "fakeHealthReporter"
    78  }
    79  
    80  func (f *fakeHealthReporter) Healthy(context.Context) error {
    81  	return f.health
    82  }