github.com/machinefi/w3bstream@v1.6.5-rc9.0.20240426031326-b8c7c4876e72/pkg/depends/conf/http/helper_liveness_checker_test.go (about)

     1  package http_test
     2  
     3  import (
     4  	"context"
     5  	"testing"
     6  
     7  	. "github.com/onsi/gomega"
     8  
     9  	. "github.com/machinefi/w3bstream/pkg/depends/conf/http"
    10  )
    11  
    12  type SomeChecker struct{}
    13  
    14  func (SomeChecker) LivenessCheck() map[string]string {
    15  	return map[string]string{
    16  		"mw1_1": "ok",
    17  		"mw1_2": "failed",
    18  	}
    19  }
    20  
    21  type WrapChecker struct{ SomeChecker }
    22  
    23  func (WrapChecker) LivenessCheck() map[string]string {
    24  	return map[string]string{
    25  		"mw1_1": "ok",
    26  		"mw1_2": "failed",
    27  		"mw2_1": "failed",
    28  		"mw2_2": "ok",
    29  	}
    30  }
    31  
    32  type Config struct {
    33  	SomeChecker
    34  	WrapChecker
    35  }
    36  
    37  type ConfigWithChecker struct {
    38  	SomeChecker
    39  	WrapChecker
    40  }
    41  
    42  func (ConfigWithChecker) LivenessCheck() map[string]string {
    43  	return map[string]string{
    44  		"SomeChecker/mw1_1": "ok",
    45  		"SomeChecker/mw1_2": "failed",
    46  		"WrapChecker/mw1_1": "ok",
    47  		"WrapChecker/mw1_2": "failed",
    48  		"WrapChecker/mw2_1": "failed",
    49  		"WrapChecker/mw2_2": "ok",
    50  	}
    51  }
    52  
    53  func TestRegisterCheckerBy(t *testing.T) {
    54  	t.Run("ForEachInputChecker", func(t *testing.T) {
    55  		RegisterCheckerBy(SomeChecker{}, WrapChecker{})
    56  
    57  		statuses, err := (&Liveness{}).Output(bgctx)
    58  		NewWithT(t).Expect(err).To(BeNil())
    59  		NewWithT(t).Expect(statuses).To(Equal(map[string]string{
    60  			"SomeChecker/mw1_1": "ok",
    61  			"SomeChecker/mw1_2": "failed",
    62  			"WrapChecker/mw1_1": "ok",
    63  			"WrapChecker/mw1_2": "failed",
    64  			"WrapChecker/mw2_1": "failed",
    65  			"WrapChecker/mw2_2": "ok",
    66  		}))
    67  	})
    68  
    69  	t.Run("ForEachField", func(t *testing.T) {
    70  		RegisterCheckerBy(Config{})
    71  
    72  		statuses, err := (&Liveness{}).Output(bgctx)
    73  		NewWithT(t).Expect(err).To(BeNil())
    74  		NewWithT(t).Expect(statuses).To(Equal(map[string]string{
    75  			"SomeChecker/mw1_1": "ok",
    76  			"SomeChecker/mw1_2": "failed",
    77  			"WrapChecker/mw1_1": "ok",
    78  			"WrapChecker/mw1_2": "failed",
    79  			"WrapChecker/mw2_1": "failed",
    80  			"WrapChecker/mw2_2": "ok",
    81  		}))
    82  	})
    83  
    84  	t.Run("CoveredSubLivenessChecker", func(t *testing.T) {
    85  		ResetRegistered()
    86  		RegisterCheckerBy(ConfigWithChecker{})
    87  
    88  		statuses, err := (&Liveness{}).Output(bgctx)
    89  		NewWithT(t).Expect(err).To(BeNil())
    90  		NewWithT(t).Expect(statuses).To(Equal(map[string]string{
    91  			"ConfigWithChecker/SomeChecker/mw1_1": "ok",
    92  			"ConfigWithChecker/SomeChecker/mw1_2": "failed",
    93  			"ConfigWithChecker/WrapChecker/mw1_1": "ok",
    94  			"ConfigWithChecker/WrapChecker/mw1_2": "failed",
    95  			"ConfigWithChecker/WrapChecker/mw2_1": "failed",
    96  			"ConfigWithChecker/WrapChecker/mw2_2": "ok",
    97  		}))
    98  	})
    99  }
   100  
   101  var bgctx = context.Background()