| // Copyright 2026 Google LLC |
| // |
| // Licensed under the Apache License, Version 2.0 (the "License"); |
| // you may not use this file except in compliance with the License. |
| // You may obtain a copy of the License at |
| // |
| // http://www.apache.org/licenses/LICENSE-2.0 |
| // |
| // Unless required by applicable law or agreed to in writing, software |
| // distributed under the License is distributed on an "AS IS" BASIS, |
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| // See the License for the specific language governing permissions and |
| // limitations under the License. |
| |
| package rsmsg |
| |
| import ( |
| "testing" |
| "time" |
| |
| rspb "google.golang.org/genproto/googleapis/devtools/resultstore/v2" |
| tspb "google.golang.org/protobuf/types/known/timestamppb" |
| |
| _ "github.com/bazelbuild/rsclient/internal/pkg/testsetup" |
| ) |
| |
| func TestValidateTimeOrder(t *testing.T) { |
| // Base time for testing. |
| t0 := time.Date(2026, 4, 15, 10, 0, 0, 0, time.UTC) |
| // One second later. |
| t1 := t0.Add(time.Second) |
| |
| tests := []struct { |
| name string |
| earlier time.Time |
| later time.Time |
| wantErr bool |
| }{ |
| { |
| name: "correct order: earlier is before later", |
| earlier: t0, |
| later: t1, |
| wantErr: false, |
| }, |
| { |
| name: "equal times: earlier is equal to later", |
| earlier: t0, |
| later: t0, |
| wantErr: false, |
| }, |
| { |
| name: "incorrect order: earlier is actually later", |
| earlier: t1, |
| later: t0, |
| wantErr: true, |
| }, |
| { |
| name: "zero earlier time: effectively ignored", |
| earlier: time.Time{}, |
| later: t1, |
| wantErr: false, |
| }, |
| { |
| name: "zero later time: effectively ignored", |
| earlier: t0, |
| later: time.Time{}, |
| wantErr: false, |
| }, |
| } |
| |
| for _, tt := range tests { |
| t.Run(tt.name, func(t *testing.T) { |
| if err := ValidateTimeOrder(tt.earlier, tt.later, "test"); (err != nil) != tt.wantErr { |
| t.Errorf("ValidateTimeOrder() error = %v, wantErr %v", err, tt.wantErr) |
| } |
| }) |
| } |
| } |
| |
| func TestValidateTimingConsistency(t *testing.T) { |
| // Protobuf timestamps for testing. |
| t0 := tspb.New(time.Date(2026, 4, 15, 10, 0, 0, 0, time.UTC)) |
| t1 := tspb.New(t0.AsTime().Add(time.Second)) |
| |
| tests := []struct { |
| name string |
| parent *rspb.Timing |
| child *rspb.Timing |
| wantErr bool |
| }{ |
| { |
| name: "consistent: child starts after parent", |
| parent: &rspb.Timing{StartTime: t0}, |
| child: &rspb.Timing{StartTime: t1}, |
| wantErr: false, |
| }, |
| { |
| name: "inconsistent: child starts before parent", |
| parent: &rspb.Timing{StartTime: t1}, |
| child: &rspb.Timing{StartTime: t0}, |
| wantErr: true, |
| }, |
| { |
| name: "nil parent: nothing to compare against, success", |
| parent: nil, |
| child: &rspb.Timing{StartTime: t0}, |
| wantErr: false, |
| }, |
| { |
| name: "nil child: nothing to validate, success", |
| parent: &rspb.Timing{StartTime: t0}, |
| child: nil, |
| wantErr: false, |
| }, |
| { |
| name: "nil parent timestamp: nothing to compare against, success", |
| parent: &rspb.Timing{StartTime: nil}, |
| child: &rspb.Timing{StartTime: t0}, |
| wantErr: false, |
| }, |
| } |
| |
| for _, tt := range tests { |
| t.Run(tt.name, func(t *testing.T) { |
| if err := ValidateTimingConsistency(tt.parent, tt.child); (err != nil) != tt.wantErr { |
| t.Errorf("ValidateTimingConsistency() error = %v, wantErr %v", err, tt.wantErr) |
| } |
| }) |
| } |
| } |
| |
| func TestClampTiming(t *testing.T) { |
| // Protobuf timestamps for testing. |
| t0 := tspb.New(time.Date(2026, 4, 15, 10, 0, 0, 0, time.UTC)) |
| t1 := tspb.New(t0.AsTime().Add(time.Second)) |
| |
| tests := []struct { |
| name string |
| parent *rspb.Timing |
| child *rspb.Timing |
| wantMod bool |
| wantChild *tspb.Timestamp |
| }{ |
| { |
| name: "no clamp needed: child is already after parent", |
| parent: &rspb.Timing{StartTime: t0}, |
| child: &rspb.Timing{StartTime: t1}, |
| wantMod: false, |
| wantChild: t1, |
| }, |
| { |
| name: "clamp needed: child starts before parent, should be moved forward", |
| parent: &rspb.Timing{StartTime: t1}, |
| child: &rspb.Timing{StartTime: t0}, |
| wantMod: true, |
| wantChild: t1, |
| }, |
| { |
| name: "nil parent: no clamping possible", |
| parent: nil, |
| child: &rspb.Timing{StartTime: t0}, |
| wantMod: false, |
| wantChild: t0, |
| }, |
| } |
| |
| for _, tt := range tests { |
| t.Run(tt.name, func(t *testing.T) { |
| gotMod := ClampTiming(tt.parent, tt.child) |
| if gotMod != tt.wantMod { |
| t.Errorf("ClampTiming() gotMod = %v, want %v", gotMod, tt.wantMod) |
| } |
| if tt.child != nil && tt.child.StartTime != nil { |
| // Verify the resulting timestamp matches our expectation. |
| if !tt.child.StartTime.AsTime().Equal(tt.wantChild.AsTime()) { |
| t.Errorf("ClampTiming() child.StartTime = %v, want %v", tt.child.StartTime, tt.wantChild) |
| } |
| } |
| }) |
| } |
| } |
| |
| func TestClampUploadRequestTimingWithClone(t *testing.T) { |
| t0 := tspb.New(time.Date(2026, 4, 15, 10, 0, 0, 0, time.UTC)) |
| t1 := tspb.New(t0.AsTime().Add(time.Second)) |
| |
| tests := []struct { |
| name string |
| parentTiming *rspb.Timing |
| req *rspb.UploadRequest |
| wantMod bool |
| }{ |
| { |
| name: "no clamp needed: child after parent", |
| parentTiming: &rspb.Timing{StartTime: t0}, |
| req: &rspb.UploadRequest{ |
| Resource: &rspb.UploadRequest_Action{ |
| Action: &rspb.Action{ |
| Timing: &rspb.Timing{StartTime: t1}, |
| }, |
| }, |
| }, |
| wantMod: false, |
| }, |
| { |
| name: "clamp needed: child before parent", |
| parentTiming: &rspb.Timing{StartTime: t1}, |
| req: &rspb.UploadRequest{ |
| Resource: &rspb.UploadRequest_Action{ |
| Action: &rspb.Action{ |
| Timing: &rspb.Timing{StartTime: t0}, |
| }, |
| }, |
| }, |
| wantMod: true, |
| }, |
| { |
| name: "not an action: skip", |
| parentTiming: &rspb.Timing{StartTime: t1}, |
| req: &rspb.UploadRequest{ |
| Resource: &rspb.UploadRequest_Invocation{ |
| Invocation: &rspb.Invocation{}, |
| }, |
| }, |
| wantMod: false, |
| }, |
| } |
| |
| for _, tt := range tests { |
| t.Run(tt.name, func(t *testing.T) { |
| origReq := tt.req |
| gotReq, gotMod := ClampUploadRequestTimingWithClone(tt.req, tt.parentTiming) |
| |
| if gotMod != tt.wantMod { |
| t.Errorf("ClampUploadRequestTimingWithClone() gotMod = %v, want %v", gotMod, tt.wantMod) |
| } |
| |
| if gotMod { |
| if gotReq == origReq { |
| t.Error("ClampUploadRequestTimingWithClone() returned original request pointer on modification, want a clone") |
| } |
| // Verify the repair was actually made on the returned request. |
| if gotReq.GetAction().Timing.StartTime.AsTime().Before(tt.parentTiming.StartTime.AsTime()) { |
| t.Errorf("ClampUploadRequestTimingWithClone() failed to repair timing: %v is still before %v", |
| gotReq.GetAction().Timing.StartTime.AsTime(), tt.parentTiming.StartTime.AsTime()) |
| } |
| } else { |
| if gotReq != origReq { |
| t.Error("ClampUploadRequestTimingWithClone() returned a different pointer when no modification was needed") |
| } |
| } |
| }) |
| } |
| } |