|
| 1 | +// Copyright 2026 The HuaTuo Authors |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package pidfile |
| 16 | + |
| 17 | +import ( |
| 18 | + "os" |
| 19 | + "path/filepath" |
| 20 | + "strconv" |
| 21 | + "testing" |
| 22 | + |
| 23 | + "github.com/stretchr/testify/assert" |
| 24 | + "github.com/stretchr/testify/require" |
| 25 | +) |
| 26 | + |
| 27 | +func TestPath(t *testing.T) { |
| 28 | + tests := []struct { |
| 29 | + name string |
| 30 | + expected string |
| 31 | + }{ |
| 32 | + {"app", "/var/run/app.pid"}, |
| 33 | + {"nginx", "/var/run/nginx.pid"}, |
| 34 | + {"", "/var/run/.pid"}, |
| 35 | + } |
| 36 | + |
| 37 | + for _, tt := range tests { |
| 38 | + t.Run(tt.name, func(t *testing.T) { |
| 39 | + assert.Equal(t, tt.expected, path(tt.name)) |
| 40 | + }) |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +func TestLock_Success(t *testing.T) { |
| 45 | + tmpDir := t.TempDir() |
| 46 | + oldDefault := defaultDirPath |
| 47 | + defaultDirPath = tmpDir |
| 48 | + t.Cleanup(func() { defaultDirPath = oldDefault }) |
| 49 | + |
| 50 | + name := "testapp" |
| 51 | + pidPath := path(name) |
| 52 | + |
| 53 | + err := Lock(name) |
| 54 | + require.NoError(t, err) |
| 55 | + t.Cleanup(func() { UnLock(name) }) |
| 56 | + |
| 57 | + _, err = os.Stat(pidPath) |
| 58 | + require.NoError(t, err) |
| 59 | + |
| 60 | + data, err := os.ReadFile(pidPath) |
| 61 | + require.NoError(t, err) |
| 62 | + pid, err := strconv.Atoi(string(data)) |
| 63 | + require.NoError(t, err) |
| 64 | + assert.Equal(t, os.Getpid(), pid) |
| 65 | +} |
| 66 | + |
| 67 | +func TestLock_AlreadyLocked(t *testing.T) { |
| 68 | + tmpDir := t.TempDir() |
| 69 | + oldDefault := defaultDirPath |
| 70 | + defaultDirPath = tmpDir |
| 71 | + t.Cleanup(func() { defaultDirPath = oldDefault }) |
| 72 | + |
| 73 | + name := "test-locked" |
| 74 | + |
| 75 | + err := Lock(name) |
| 76 | + require.NoError(t, err) |
| 77 | + t.Cleanup(func() { UnLock(name) }) |
| 78 | + |
| 79 | + err = Lock(name) |
| 80 | + assert.Error(t, err) |
| 81 | + assert.Contains(t, err.Error(), "running") |
| 82 | +} |
| 83 | + |
| 84 | +func TestUnLock(t *testing.T) { |
| 85 | + tmpDir := t.TempDir() |
| 86 | + oldDefault := defaultDirPath |
| 87 | + defaultDirPath = tmpDir |
| 88 | + t.Cleanup(func() { defaultDirPath = oldDefault }) |
| 89 | + |
| 90 | + name := "toremove" |
| 91 | + pidPath := path(name) |
| 92 | + |
| 93 | + err := os.WriteFile(pidPath, []byte("12345"), 0o600) |
| 94 | + require.NoError(t, err) |
| 95 | + |
| 96 | + UnLock(name) |
| 97 | + |
| 98 | + _, err = os.Stat(pidPath) |
| 99 | + assert.True(t, os.IsNotExist(err)) |
| 100 | +} |
| 101 | + |
| 102 | +func TestRead(t *testing.T) { |
| 103 | + tmp := t.TempDir() |
| 104 | + pidPath := filepath.Join(tmp, "test.pid") |
| 105 | + |
| 106 | + tests := []struct { |
| 107 | + name string |
| 108 | + content string |
| 109 | + wantPID int |
| 110 | + wantErrKind bool // 是否期望有错误 |
| 111 | + }{ |
| 112 | + {"normal", "12345", 12345, false}, |
| 113 | + {"with space", " 67890 \n", 67890, false}, |
| 114 | + {"negative", "-1", -1, false}, |
| 115 | + {"empty", "", 0, true}, |
| 116 | + {"invalid", "abc", 0, true}, |
| 117 | + } |
| 118 | + |
| 119 | + for _, tt := range tests { |
| 120 | + t.Run(tt.name, func(t *testing.T) { |
| 121 | + err := os.WriteFile(pidPath, []byte(tt.content), 0o600) |
| 122 | + require.NoError(t, err) |
| 123 | + |
| 124 | + got, err := Read(pidPath) |
| 125 | + if tt.wantErrKind { |
| 126 | + assert.Error(t, err) |
| 127 | + assert.Zero(t, got) |
| 128 | + } else { |
| 129 | + assert.NoError(t, err) |
| 130 | + assert.Equal(t, tt.wantPID, got) |
| 131 | + } |
| 132 | + }) |
| 133 | + } |
| 134 | +} |
| 135 | + |
| 136 | +func TestRead_NotExist(t *testing.T) { |
| 137 | + _, err := Read("/this/file/does/not/exist.pid") |
| 138 | + assert.Error(t, err) |
| 139 | + assert.True(t, os.IsNotExist(err)) |
| 140 | +} |
0 commit comments