Skip to content

Commit

Permalink
tests
Browse files Browse the repository at this point in the history
Signed-off-by: Tim Vaillancourt <[email protected]>
  • Loading branch information
timvaillancourt committed Sep 9, 2024
1 parent 16a2aed commit a088608
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
12 changes: 12 additions & 0 deletions go/vt/discovery/healthcheck_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"vitess.io/vitess/go/protoutil"
"vitess.io/vitess/go/test/utils"
"vitess.io/vitess/go/vt/grpcclient"
"vitess.io/vitess/go/vt/topo"
Expand Down Expand Up @@ -76,6 +77,7 @@ func TestHealthCheck(t *testing.T) {
tablet.Type = topodatapb.TabletType_REPLICA
input := make(chan *querypb.StreamHealthResponse)
conn := createFakeConn(tablet, input)
now := time.Now()

// create a channel and subscribe to healthcheck
resultChan := hc.Subscribe()
Expand All @@ -90,6 +92,7 @@ func TestHealthCheck(t *testing.T) {
Serving: false,
Stats: nil,
PrimaryTermStartTime: 0,
Timestamp: protoutil.TimeToProto(now),
}
result := <-resultChan
mustMatch(t, want, result, "Wrong TabletHealth data")
Expand All @@ -101,6 +104,7 @@ func TestHealthCheck(t *testing.T) {

PrimaryTermStartTimestamp: 0,
RealtimeStats: &querypb.RealtimeStats{ReplicationLagSeconds: 1, CpuUsage: 0.5},
Timestamp: protoutil.TimeToProto(now),
}
input <- shr
result = <-resultChan
Expand All @@ -110,6 +114,7 @@ func TestHealthCheck(t *testing.T) {
Serving: true,
Stats: &querypb.RealtimeStats{ReplicationLagSeconds: 1, CpuUsage: 0.5},
PrimaryTermStartTime: 0,
Timestamp: protoutil.TimeToProto(now),
}
// create a context with timeout and select on it and channel
mustMatch(t, want, result, "Wrong TabletHealth data")
Expand All @@ -124,6 +129,7 @@ func TestHealthCheck(t *testing.T) {
Serving: true,
Stats: &querypb.RealtimeStats{ReplicationLagSeconds: 1, CpuUsage: 0.5},
PrimaryTermStartTime: 0,
Timestamp: protoutil.TimeToProto(now),
}},
}}
// we can't use assert.Equal here because of the special way we want to compare equality
Expand All @@ -137,6 +143,7 @@ func TestHealthCheck(t *testing.T) {
Serving: true,
PrimaryTermStartTimestamp: 10,
RealtimeStats: &querypb.RealtimeStats{ReplicationLagSeconds: 1, CpuUsage: 0.2},
Timestamp: protoutil.TimeToProto(now),
}
want = &TabletHealth{
Tablet: tablet,
Expand All @@ -149,6 +156,7 @@ func TestHealthCheck(t *testing.T) {
Conn: conn,
Stats: &querypb.RealtimeStats{ReplicationLagSeconds: 1, CpuUsage: 0.2},
PrimaryTermStartTime: 10,
Timestamp: protoutil.TimeToProto(now),
}
input <- shr
result = <-resultChan
Expand All @@ -166,13 +174,15 @@ func TestHealthCheck(t *testing.T) {
Serving: false,
PrimaryTermStartTimestamp: 0,
RealtimeStats: &querypb.RealtimeStats{ReplicationLagSeconds: 1, CpuUsage: 0.3},
Timestamp: protoutil.TimeToProto(now),
}
want = &TabletHealth{
Tablet: tablet,
Target: &querypb.Target{Keyspace: "k", Shard: "s", TabletType: topodatapb.TabletType_REPLICA},
Serving: false,
Stats: &querypb.RealtimeStats{ReplicationLagSeconds: 1, CpuUsage: 0.3},
PrimaryTermStartTime: 0,
Timestamp: protoutil.TimeToProto(now),
}
input <- shr
result = <-resultChan
Expand All @@ -186,6 +196,7 @@ func TestHealthCheck(t *testing.T) {
Serving: true,
PrimaryTermStartTimestamp: 0,
RealtimeStats: &querypb.RealtimeStats{HealthError: "some error", ReplicationLagSeconds: 1, CpuUsage: 0.3},
Timestamp: protoutil.TimeToProto(now),
}
want = &TabletHealth{
Tablet: tablet,
Expand All @@ -194,6 +205,7 @@ func TestHealthCheck(t *testing.T) {
Stats: &querypb.RealtimeStats{HealthError: "some error", ReplicationLagSeconds: 1, CpuUsage: 0.3},
PrimaryTermStartTime: 0,
LastError: fmt.Errorf("vttablet error: some error"),
Timestamp: protoutil.TimeToProto(now),
}
input <- shr
result = <-resultChan
Expand Down
3 changes: 3 additions & 0 deletions go/vt/discovery/tablet_health_check.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
"google.golang.org/grpc"
"google.golang.org/protobuf/proto"

"vitess.io/vitess/go/protoutil"
"vitess.io/vitess/go/vt/grpcclient"
"vitess.io/vitess/go/vt/log"
"vitess.io/vitess/go/vt/proto/query"
Expand Down Expand Up @@ -100,6 +101,7 @@ func (thc *tabletHealthCheck) SimpleCopy() *TabletHealth {
LastError: thc.LastError,
PrimaryTermStartTime: thc.PrimaryTermStartTime,
Serving: thc.Serving,
Timestamp: thc.Timestamp,
}
}

Expand All @@ -126,6 +128,7 @@ func (thc *tabletHealthCheck) setServingState(serving bool, reason string) {
thc.loggedServingState = true
}
thc.Serving = serving
thc.Timestamp = protoutil.TimeToProto(time.Now())
}

// stream streams healthcheck responses to callback.
Expand Down

0 comments on commit a088608

Please sign in to comment.