golang.org/x/build@v0.0.0-20240506185731-218518f32b70/maintner/maintnerd/gcslog/gcslog_test.go (about)

     1  // Copyright 2017 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package gcslog
     6  
     7  import (
     8  	"context"
     9  	"testing"
    10  	"time"
    11  
    12  	"golang.org/x/build/maintner/maintpb"
    13  )
    14  
    15  func TestGCSLogWakeup_Timeout(t *testing.T) {
    16  	testGCSLogWakeup(t, false)
    17  }
    18  
    19  func TestGCSLogWakeup_Activity(t *testing.T) {
    20  	testGCSLogWakeup(t, true)
    21  }
    22  
    23  func testGCSLogWakeup(t *testing.T, activity bool) {
    24  	gl := newGCSLogBase()
    25  	waitc := make(chan bool, 1)
    26  	ctx, cancel := context.WithTimeout(context.Background(), 500*time.Millisecond)
    27  	defer cancel()
    28  	go func() {
    29  		waitc <- gl.waitSizeNot(ctx, 0)
    30  	}()
    31  	if activity {
    32  		if err := gl.Log(new(maintpb.Mutation)); err != nil {
    33  			t.Fatal(err)
    34  		}
    35  	}
    36  	select {
    37  	case got := <-waitc:
    38  		if got != activity {
    39  			t.Errorf("changed = %v; want %v", got, activity)
    40  		}
    41  	case <-time.After(2 * time.Second):
    42  		t.Errorf("timeout")
    43  	}
    44  }