Skip to content

Commit c332a3f

Browse files
committed
fix: count instances of runmqdevserver correctly on M1 macs
With emulation runmqdevserver appears multiple times in a single line of `ps` output, so we need to count the number of lines with runmqdevserver instead. Issue: ibm-messaging#476
1 parent 96135e8 commit c332a3f

File tree

2 files changed

+41
-3
lines changed

2 files changed

+41
-3
lines changed

cmd/runmqserver/process.go

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,14 +44,23 @@ func verifySingleProcess() error {
4444
func verifyOnlyOne(programName string) (int, error) {
4545
// #nosec G104
4646
out, _, _ := command.Run("ps", "-e", "--format", "cmd")
47-
//if this goes wrong then assume we are the only one
48-
numOfProg := strings.Count(out, programName)
49-
if numOfProg != 1 {
47+
lines := strings.Split(out, "\n")
48+
if countLinesWith(lines, programName) > 1 {
5049
return numOfProg, fmt.Errorf("Expected there to be only 1 instance of %s but found %d", programName, numOfProg)
5150
}
5251
return numOfProg, nil
5352
}
5453

54+
func countLinesWith(lines []string, pattern string) int {
55+
var occurances int
56+
for i := range lines {
57+
if strings.Contains(lines[i], pattern) {
58+
occurances += 1
59+
}
60+
}
61+
return occurances
62+
}
63+
5564
// Determines the name of the currently running executable.
5665
func determineExecutable() (string, error) {
5766
file, err := os.Executable()

cmd/runmqserver/process_test.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
© Copyright IBM Corporation 2017
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE_2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
package main
17+
18+
import "testing"
19+
20+
func TestCountLinesWith(t *testing.T) {
21+
lines := []string{
22+
"/usr/bin/qemu-x86_x64 /usr/local/bin/runmqdevserver runmqdevserver",
23+
"date -u",
24+
}
25+
got := countLinesWith(lines, "runmqdevserver")
26+
if got != 1 {
27+
t.Fatalf("got %d occurances", got)
28+
}
29+
}

0 commit comments

Comments
 (0)