code.gitea.io/gitea@v1.19.3/modules/process/manager_test.go (about) 1 // Copyright 2020 The Gitea Authors. All rights reserved. 2 // SPDX-License-Identifier: MIT 3 4 package process 5 6 import ( 7 "context" 8 "testing" 9 "time" 10 11 "github.com/stretchr/testify/assert" 12 ) 13 14 func TestGetManager(t *testing.T) { 15 go func() { 16 // test race protection 17 _ = GetManager() 18 }() 19 pm := GetManager() 20 assert.NotNil(t, pm) 21 } 22 23 func TestManager_AddContext(t *testing.T) { 24 pm := Manager{processMap: make(map[IDType]*process), next: 1} 25 26 ctx, cancel := context.WithCancel(context.Background()) 27 defer cancel() 28 29 p1Ctx, _, finished := pm.AddContext(ctx, "foo") 30 defer finished() 31 assert.NotEmpty(t, GetContext(p1Ctx).GetPID(), "expected to get non-empty pid") 32 33 p2Ctx, _, finished := pm.AddContext(p1Ctx, "bar") 34 defer finished() 35 36 assert.NotEmpty(t, GetContext(p2Ctx).GetPID(), "expected to get non-empty pid") 37 38 assert.NotEqual(t, GetContext(p1Ctx).GetPID(), GetContext(p2Ctx).GetPID(), "expected to get different pids %s == %s", GetContext(p2Ctx).GetPID(), GetContext(p1Ctx).GetPID()) 39 assert.Equal(t, GetContext(p1Ctx).GetPID(), GetContext(p2Ctx).GetParent().GetPID(), "expected to get pid %s got %s", GetContext(p1Ctx).GetPID(), GetContext(p2Ctx).GetParent().GetPID()) 40 } 41 42 func TestManager_Cancel(t *testing.T) { 43 pm := Manager{processMap: make(map[IDType]*process), next: 1} 44 45 ctx, _, finished := pm.AddContext(context.Background(), "foo") 46 defer finished() 47 48 pm.Cancel(GetPID(ctx)) 49 50 select { 51 case <-ctx.Done(): 52 default: 53 assert.Fail(t, "Cancel should cancel the provided context") 54 } 55 finished() 56 57 ctx, cancel, finished := pm.AddContext(context.Background(), "foo") 58 defer finished() 59 60 cancel() 61 62 select { 63 case <-ctx.Done(): 64 default: 65 assert.Fail(t, "Cancel should cancel the provided context") 66 } 67 finished() 68 } 69 70 func TestManager_Remove(t *testing.T) { 71 pm := Manager{processMap: make(map[IDType]*process), next: 1} 72 73 ctx, cancel := context.WithCancel(context.Background()) 74 defer cancel() 75 76 p1Ctx, _, finished := pm.AddContext(ctx, "foo") 77 defer finished() 78 assert.NotEmpty(t, GetContext(p1Ctx).GetPID(), "expected to have non-empty PID") 79 80 p2Ctx, _, finished := pm.AddContext(p1Ctx, "bar") 81 defer finished() 82 83 assert.NotEqual(t, GetContext(p1Ctx).GetPID(), GetContext(p2Ctx).GetPID(), "expected to get different pids got %s == %s", GetContext(p2Ctx).GetPID(), GetContext(p1Ctx).GetPID()) 84 85 finished() 86 87 _, exists := pm.processMap[GetPID(p2Ctx)] 88 assert.False(t, exists, "PID %d is in the list but shouldn't", GetPID(p2Ctx)) 89 } 90 91 func TestExecTimeoutNever(t *testing.T) { 92 // TODO Investigate how to improve the time elapsed per round. 93 maxLoops := 10 94 for i := 1; i < maxLoops; i++ { 95 _, stderr, err := GetManager().ExecTimeout(5*time.Second, "ExecTimeout", "git", "--version") 96 if err != nil { 97 t.Fatalf("git --version: %v(%s)", err, stderr) 98 } 99 } 100 } 101 102 func TestExecTimeoutAlways(t *testing.T) { 103 maxLoops := 100 104 for i := 1; i < maxLoops; i++ { 105 _, stderr, err := GetManager().ExecTimeout(100*time.Microsecond, "ExecTimeout", "sleep", "5") 106 // TODO Simplify logging and errors to get precise error type. E.g. checking "if err != context.DeadlineExceeded". 107 if err == nil { 108 t.Fatalf("sleep 5 secs: %v(%s)", err, stderr) 109 } 110 } 111 }