-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
http2, internal/gate: move Gate type to an internal package
For reuse in internal/http3. Change-Id: I186d7219194a07c100aa8bd049e007232de2d3d9 Reviewed-on: https://go-review.googlesource.com/c/net/+/641497 Auto-Submit: Damien Neil <dneil@google.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Reviewed-by: Jonathan Amsterdam <jba@google.com>
- Loading branch information
Showing
3 changed files
with
112 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
// Copyright 2024 The Go Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style | ||
// license that can be found in the LICENSE file. | ||
|
||
package gate_test | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
"time" | ||
|
||
"golang.org/x/net/internal/gate" | ||
) | ||
|
||
func TestGateLockAndUnlock(t *testing.T) { | ||
g := gate.New(false) | ||
if set := g.Lock(); set { | ||
t.Errorf("g.Lock of never-locked gate: true, want false") | ||
} | ||
unlockedc := make(chan struct{}) | ||
donec := make(chan struct{}) | ||
go func() { | ||
defer close(donec) | ||
if set := g.Lock(); !set { | ||
t.Errorf("g.Lock of set gate: false, want true") | ||
} | ||
select { | ||
case <-unlockedc: | ||
default: | ||
t.Errorf("g.Lock succeeded while gate was held") | ||
} | ||
g.Unlock(false) | ||
}() | ||
time.Sleep(1 * time.Millisecond) | ||
close(unlockedc) | ||
g.Unlock(true) | ||
<-donec | ||
if set := g.Lock(); set { | ||
t.Errorf("g.Lock of unset gate: true, want false") | ||
} | ||
} | ||
|
||
func TestGateWaitAndLock(t *testing.T) { | ||
g := gate.New(false) | ||
// WaitAndLock is canceled. | ||
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Millisecond) | ||
defer cancel() | ||
if err := g.WaitAndLock(ctx); err != context.DeadlineExceeded { | ||
t.Fatalf("g.WaitAndLock = %v, want context.DeadlineExceeded", err) | ||
} | ||
// WaitAndLock succeeds. | ||
set := false | ||
go func() { | ||
time.Sleep(1 * time.Millisecond) | ||
g.Lock() | ||
set = true | ||
g.Unlock(true) | ||
}() | ||
if err := g.WaitAndLock(context.Background()); err != nil { | ||
t.Fatalf("g.WaitAndLock = %v, want nil", err) | ||
} | ||
if !set { | ||
t.Fatalf("g.WaitAndLock returned before gate was set") | ||
} | ||
g.Unlock(true) | ||
// WaitAndLock succeeds when the gate is set and the context is canceled. | ||
if err := g.WaitAndLock(ctx); err != nil { | ||
t.Fatalf("g.WaitAndLock = %v, want nil", err) | ||
} | ||
} | ||
|
||
func TestGateLockIfSet(t *testing.T) { | ||
g := gate.New(false) | ||
if locked := g.LockIfSet(); locked { | ||
t.Fatalf("g.LockIfSet of unset gate = %v, want false", locked) | ||
} | ||
g.Lock() | ||
if locked := g.LockIfSet(); locked { | ||
t.Fatalf("g.LockIfSet of locked gate = %v, want false", locked) | ||
} | ||
g.Unlock(true) | ||
if locked := g.LockIfSet(); !locked { | ||
t.Fatalf("g.LockIfSet of set gate = %v, want true", locked) | ||
} | ||
} |