go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/cv/internal/gerrit/poller/notify_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 poller
    16  
    17  import (
    18  	"sort"
    19  	"testing"
    20  
    21  	"go.chromium.org/luci/cv/internal/changelist"
    22  	"go.chromium.org/luci/cv/internal/common"
    23  	"go.chromium.org/luci/cv/internal/cvtesting"
    24  
    25  	. "github.com/smartystreets/goconvey/convey"
    26  )
    27  
    28  func TestNotifyOnUnmatchedCLs(t *testing.T) {
    29  	t.Parallel()
    30  
    31  	Convey("notifyOnUnmatchedCLs works", t, func() {
    32  		ct := cvtesting.Test{}
    33  		ctx, cancel := ct.SetUp(t)
    34  		defer cancel()
    35  
    36  		const lProject = "chromium"
    37  		const gHost = "chromium-review.example.com"
    38  		const gRepo = "infra/infra"
    39  
    40  		pm := pmMock{}
    41  		clUpdater := clUpdaterMock{}
    42  		p := New(ct.TQDispatcher, ct.GFactory(), &clUpdater, &pm)
    43  
    44  		changes := []int64{1, 2, 3, 4, 5}
    45  		const notYetSaved = 4
    46  
    47  		var knownIDs common.CLIDs
    48  		for _, i := range changes {
    49  			if i == notYetSaved {
    50  				continue
    51  			}
    52  			cl := changelist.MustGobID(gHost, i).MustCreateIfNotExists(ctx)
    53  			// In practice, cl.Snapshot would be populated, but for this test it
    54  			// doesn't matter.
    55  			knownIDs = append(knownIDs, cl.ID)
    56  		}
    57  		sort.Sort(knownIDs)
    58  
    59  		err := p.notifyOnUnmatchedCLs(ctx, lProject, gHost, changes, changelist.UpdateCLTask_RUN_POKE)
    60  		So(err, ShouldBeNil)
    61  
    62  		// PM must be notified immediately on CLs already saved.
    63  		ids := pm.projects[lProject]
    64  		sort.Sort(ids)
    65  		So(ids, ShouldResemble, knownIDs)
    66  
    67  		// CL Updater must have scheduled tasks.
    68  		etas := clUpdater.peekETAs()
    69  		payloads := clUpdater.popPayloadsByETA()
    70  		So(payloads, ShouldHaveLength, len(changes))
    71  		// Tasks must be somewhat distributed in time.
    72  		mid := ct.Clock.Now().Add(fullPollInterval / 2)
    73  		So(etas[1], ShouldHappenBefore, mid)
    74  		So(etas[3], ShouldHappenAfter, mid)
    75  	})
    76  }