go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/resultdb/internal/integrationtests/invocation_finalization_test.go (about)

     1  // Copyright 2020 The LUCI Authors.
     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 integrationtests
    16  
    17  import (
    18  	"context"
    19  	"testing"
    20  	"time"
    21  
    22  	"go.chromium.org/luci/server/tq"
    23  
    24  	"go.chromium.org/luci/resultdb/internal/testutil"
    25  	pb "go.chromium.org/luci/resultdb/proto/v1"
    26  
    27  	. "github.com/smartystreets/goconvey/convey"
    28  )
    29  
    30  func TestInvocationFinalization(t *testing.T) {
    31  	// https://crbug.com/1116284
    32  	SkipConvey(`ShouldFinalize`, t, func() {
    33  		ctx := testutil.SpannerTestContext(t)
    34  
    35  		// Cancel the test after 20 sec.
    36  		ctx, cancel := context.WithTimeout(ctx, 20*time.Second)
    37  		defer cancel()
    38  
    39  		// Setup Cloud Tasks fake to pump messages between servers.
    40  		ctx, sched := tq.TestingContext(ctx, nil)
    41  		go sched.Run(ctx)
    42  
    43  		app, err := startTestApp(ctx)
    44  		So(err, ShouldBeNil)
    45  		defer app.Shutdown()
    46  		c := testClient{app: app}
    47  
    48  		// Create invocations A, B, C.
    49  		// A includes B. B includes C.
    50  		c.CreateInvocation(ctx, "u-a")
    51  		c.CreateInvocation(ctx, "u-b")
    52  		c.CreateInvocation(ctx, "u-c")
    53  		c.Include(ctx, "invocations/u-a", "invocations/u-b")
    54  		c.Include(ctx, "invocations/u-b", "invocations/u-c")
    55  
    56  		// Finalize A, B and C.
    57  		c.FinalizeInvocation(ctx, "invocations/u-a")
    58  		So(c.GetState(ctx, "invocations/u-a"), ShouldEqual, pb.Invocation_FINALIZING)
    59  		c.FinalizeInvocation(ctx, "invocations/u-b")
    60  		So(c.GetState(ctx, "invocations/u-b"), ShouldEqual, pb.Invocation_FINALIZING)
    61  		c.FinalizeInvocation(ctx, "invocations/u-c")
    62  
    63  		// Assert that all three invocations are finalized within 10 seconds.
    64  		for {
    65  			time.Sleep(100 * time.Millisecond)
    66  			if c.GetState(ctx, "invocations/u-a") != pb.Invocation_FINALIZED {
    67  				continue
    68  			}
    69  			if c.GetState(ctx, "invocations/u-b") != pb.Invocation_FINALIZED {
    70  				continue
    71  			}
    72  			if c.GetState(ctx, "invocations/u-c") != pb.Invocation_FINALIZED {
    73  				continue
    74  			}
    75  
    76  			break
    77  		}
    78  	})
    79  
    80  	SkipConvey(`ShouldExpire`, t, func() {
    81  		ctx := testutil.SpannerTestContext(t)
    82  
    83  		// Cancel the test after 30 sec.
    84  		ctx, cancel := context.WithTimeout(ctx, 30*time.Second)
    85  		defer cancel()
    86  
    87  		// Setup Cloud Tasks fake to pump messages between servers.
    88  		ctx, sched := tq.TestingContext(ctx, nil)
    89  		go sched.Run(ctx)
    90  
    91  		app, err := startTestApp(ctx)
    92  		So(err, ShouldBeNil)
    93  		defer app.Shutdown()
    94  		c := testClient{app: app}
    95  
    96  		// Create invocations A, B, C.
    97  		// A includes B. B includes C.
    98  		c.CreateInvocation(ctx, "u-a")
    99  		c.CreateInvocation(ctx, "u-b")
   100  		c.CreateInvocation(ctx, "u-c")
   101  		c.Include(ctx, "invocations/u-a", "invocations/u-b")
   102  		c.Include(ctx, "invocations/u-b", "invocations/u-c")
   103  
   104  		// Expire A, B and C.
   105  		c.MakeInvocationOverdue(ctx, "invocations/u-a")
   106  		c.MakeInvocationOverdue(ctx, "invocations/u-b")
   107  		c.MakeInvocationOverdue(ctx, "invocations/u-c")
   108  
   109  		// Assert that all three invocations are finalized before the context
   110  		// times out.
   111  		for {
   112  			time.Sleep(100 * time.Millisecond)
   113  			if c.GetState(ctx, "invocations/u-a") != pb.Invocation_FINALIZED {
   114  				continue
   115  			}
   116  			if c.GetState(ctx, "invocations/u-b") != pb.Invocation_FINALIZED {
   117  				continue
   118  			}
   119  			if c.GetState(ctx, "invocations/u-c") != pb.Invocation_FINALIZED {
   120  				continue
   121  			}
   122  
   123  			break
   124  		}
   125  	})
   126  }