GitLabgitlab-org
cli
main
1//go:build !integration
3package config
5import (
6 "bufio"
7 "fmt"
8 "os"
9 "os/exec"
10 "testing"
11 "time"
13 "github.com/stretchr/testify/assert"
14 "github.com/stretchr/testify/require"
15)
17// lockHoldEnv, when set, makes TestFileLock_CrossProcess run as the child: it
18// acquires the lock for that directory, announces it, and holds it until its
19// stdin closes. This lets the parent exercise real cross-process contention by
20// re-executing the test binary.
21const lockHoldEnv = "GLAB_TEST_LOCK_HOLD_DIR"
23func TestFileLock_CrossProcess(t *testing.T) {
24 if dir := os.Getenv(lockHoldEnv); dir != "" {
25 lock, err := acquireLock(dir)
26 if err != nil || lock == nil {
27 fmt.Println("error")
28 os.Exit(3)
29 }
30 fmt.Println("locked")
31 // Hold the lock until the parent closes our stdin, then release.
32 _, _ = bufio.NewReader(os.Stdin).ReadString('\n')
33 lock.release()
34 os.Exit(0)
35 }
37 dir := t.TempDir()
39 child := exec.Command(os.Args[0], "-test.run", "^TestFileLock_CrossProcess$")
40 child.Env = append(os.Environ(), lockHoldEnv+"="+dir)
41 stdin, err := child.StdinPipe()
42 require.NoError(t, err)
43 stdout, err := child.StdoutPipe()
44 require.NoError(t, err)
45 require.NoError(t, child.Start())
46 // Kill before Wait: if an assertion below fails before stdin is closed, the
47 // child is still blocked reading stdin for an EOF that never arrives, so a
48 // bare Wait() in cleanup would hang the whole test run instead of failing.
49 t.Cleanup(func() {
50 _ = child.Process.Kill()
51 _ = child.Wait()
52 })
54 // Wait until the child reports it holds the lock.
55 line, err := bufio.NewReader(stdout).ReadString('\n')
56 require.NoError(t, err)
57 require.Contains(t, line, "locked")
59 // The parent must not be able to take the lock the child holds.
60 restore := shortenLockTimeout(t, 150*time.Millisecond)
62 start := time.Now()
63 lock, err := acquireLock(dir)
64 require.ErrorIs(t, err, errLockTimeout)
65 require.Nil(t, lock)
66 require.GreaterOrEqual(t, time.Since(start), 150*time.Millisecond)
68 // Restore the default timeout before probing so each acquireLock below returns
69 // as soon as the child releases the lock rather than blocking for 150 ms.
70 restore()
72 // Releasing the child (closing its stdin) frees the lock so we can take it.
73 require.NoError(t, stdin.Close())
74 require.Eventually(t, func() bool {
75 l, err := acquireLock(dir)
76 if err != nil || l == nil {
77 return false
78 }
79 l.release()
80 return true
81 }, 2*time.Second, 20*time.Millisecond)
82}
84func TestAcquireLock_ReleaseAllowsReacquire(t *testing.T) {
85 dir := t.TempDir()
87 l1, err := acquireLock(dir)
88 require.NoError(t, err)
89 require.NotNil(t, l1)
90 l1.release()
92 l2, err := acquireLock(dir)
93 require.NoError(t, err)
94 require.NotNil(t, l2)
95 l2.release()
96}
98func TestAcquireLock_EmptyDirIsNoop(t *testing.T) {
99 lock, err := acquireLock("")
100 require.NoError(t, err)
101 require.Nil(t, lock)
102 // release on a nil lock must not panic.
103 lock.release()
104}
106func TestWithLock_RunsUnlockedWhenLockUnavailable(t *testing.T) {
107 dir := t.TempDir()
109 held, err := acquireLock(dir)
110 require.NoError(t, err)
111 defer held.release()
113 shortenLockTimeout(t, 100*time.Millisecond) // restored via t.Cleanup
115 ran := false
116 err = withLock(dir, func() error {
117 ran = true
118 return nil
119 })
120 require.NoError(t, err)
121 assert.True(t, ran, "withLock should run fn even when the lock is unavailable")
122}
124// shortenLockTimeout temporarily shrinks the global lockTimeout and returns a
125// function to restore it early. Restoration is also registered with t.Cleanup,
126// so the global is always reset even if the caller never invokes the returned
127// function (for example, when an assertion fails first).
128func shortenLockTimeout(t *testing.T, d time.Duration) func() {
129 t.Helper()
130 old := lockTimeout
131 lockTimeout = d
132 restore := func() { lockTimeout = old }
133 t.Cleanup(restore)
134 return restore
135}
Diagnostics6 git calls · 29 ms
CallStatusSizeTime029 ms
discover200185 B4 ms
git.upload-pack discover http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=2 git.response.bytes=185 git.server.agent=git/2.56.g1891de4-Linux git.server.object_format=sha1 git.server.caps=agent ls-refs fetch server-option object-format git.server.fetch=shallow wait-for-done filter git.protocol.version=2 git.pkt.frames=9 git.pkt.data=7 git.pkt.flush=2 git.pkt.delim=0 git.pkt.bytes=185 git.pkt.payload.max=35
ls-refs20058 KiB3 ms
git.upload-pack ls-refs http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=ls-refs git.upstream.dur_ms=3 git.response.bytes=59849 git.response.cache=UPDATING git.response.duration_ms=3 git.protocol.version=2 git.pkt.frames=731 git.pkt.data=730 git.pkt.flush=1 git.pkt.delim=0 git.pkt.bytes=59849 git.pkt.payload.max=143 git.ls_refs.count=730 git.ls_refs.head=refs/heads/main
fetchtree:0 · deepen 1020032 KiB · hit2 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=3 git.response.bytes=32926 git.response.cache=HIT git.response.duration_ms=2 git.fetch.filter=tree:0 git.fetch.wants=0dd7574dce9e385c5c30540e6c15657adc6bdce9 git.fetch.haves.count=0 git.fetch.deepen=10 git.fetch.thin_pack=false git.fetch.ofs_delta=true git.protocol.version=2 git.pkt.frames=22 git.pkt.data=20 git.pkt.flush=1 git.pkt.delim=1 git.pkt.bytes=32926 git.pkt.payload.max=32046 git.sideband.band1.bytes=32046 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=80 git.pack.bytes=32046 git.pack.ofs_delta=36 git.pack.ref_delta=0 git.pack.chain.max=2 git.pack.trailer=cb0bad5d7ffc514895d8b21c4dd470327f90bc91 git.pack.full=44Open pack map
fetchcombine:blob:none+tree:2 · deepen 12004.2 KiB · hit3 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=3 git.response.bytes=4273 git.response.cache=HIT git.response.duration_ms=3 git.fetch.filter=combine:blob:none+tree:2 git.fetch.wants=0dd7574dce9e385c5c30540e6c15657adc6bdce9 git.fetch.haves.count=0 git.fetch.deepen=1 git.fetch.thin_pack=false git.fetch.ofs_delta=true git.protocol.version=2 git.pkt.frames=7 git.pkt.data=5 git.pkt.flush=1 git.pkt.delim=1 git.pkt.bytes=4273 git.pkt.payload.max=4173 git.sideband.band1.bytes=4173 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=11 git.pack.bytes=4173 git.fetch.wide.objects=11 git.fetch.wide.extra_bytes=3930 git.fetch.wide.blobs=0
fetchtree:02001.4 KiB · hit3 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=19 git.response.bytes=1391 git.response.cache=HIT git.response.duration_ms=3 git.fetch.filter=tree:0 git.fetch.wants=e9da9edfdcf44933ee5fa121d411de326220161c git.fetch.haves.count=0 git.fetch.deepen=0 git.fetch.thin_pack=false git.fetch.ofs_delta=true git.protocol.version=2 git.pkt.frames=4 git.pkt.data=3 git.pkt.flush=1 git.pkt.delim=0 git.pkt.bytes=1391 git.pkt.payload.max=1364 git.sideband.band1.bytes=1364 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=1 git.pack.bytes=1364 git.pack.trailer=bf750bd385ed98d15e1fea1289d3b00caa61d64cOpen pack map
fetch2001.6 KiB · hit19 ms
git.upload-pack fetch http.response.status_code=200 git.response.bytes=1608 git.response.cache=HIT git.response.duration_ms=19 git.fetch.wants=2ce10a8f47f6b1f269975de7a9ea840fa376f9fb git.fetch.haves.count=0 git.fetch.deepen=0 git.fetch.thin_pack=false git.fetch.ofs_delta=true git.protocol.version=2 git.pkt.frames=4 git.pkt.data=3 git.pkt.flush=1 git.pkt.delim=0 git.pkt.bytes=1608 git.pkt.payload.max=1581 git.sideband.band1.bytes=1581 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0
Each row is one HTTP round trip ply made to the git server for this page, on one timeline. discover asks what the server speaks, ls-refs lists branch tips, fetch brings objects by id. A hit was served by Workers Cache. Tap a row for its wire details.
- Remote
- https://gitlab.com/gitlab-org/cli
- Ref
- refs/heads/main
- Commit
- 0dd7574dce9e385c5c30540e6c15657adc6bdce9
- cf-ray
- a5e265405a9adf63
- Request
- 654684b8…
- Protocol
- v2 · sha1
- Agent
- git/2.56.g1891de4-Linux
- Filter
- advertised
- Fetch
- shallow wait-for-done filter
- Capabilities
- agent ls-refs fetch server-option object-format