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