get.pme.sh/pnats@v0.0.0-20240304004023-26bb5a137ed0/test/pid_test.go (about)

     1  // Copyright 2012-2019 The NATS Authors
     2  // Licensed under the Apache License, Version 2.0 (the "License");
     3  // you may not use this file except in compliance with the License.
     4  // You may obtain a copy of the License at
     5  //
     6  // http://www.apache.org/licenses/LICENSE-2.0
     7  //
     8  // Unless required by applicable law or agreed to in writing, software
     9  // distributed under the License is distributed on an "AS IS" BASIS,
    10  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    11  // See the License for the specific language governing permissions and
    12  // limitations under the License.
    13  
    14  package test
    15  
    16  import (
    17  	"fmt"
    18  	"os"
    19  	"testing"
    20  )
    21  
    22  func TestPidFile(t *testing.T) {
    23  	opts := DefaultTestOptions
    24  
    25  	file := createTempFile(t, "nats-server:pid_")
    26  	file.Close()
    27  	opts.PidFile = file.Name()
    28  
    29  	s := RunServer(&opts)
    30  	s.Shutdown()
    31  
    32  	buf, err := os.ReadFile(opts.PidFile)
    33  	if err != nil {
    34  		t.Fatalf("Could not read pid_file: %v", err)
    35  	}
    36  	if len(buf) <= 0 {
    37  		t.Fatal("Expected a non-zero length pid_file")
    38  	}
    39  
    40  	pid := 0
    41  	fmt.Sscanf(string(buf), "%d", &pid)
    42  	if pid != os.Getpid() {
    43  		t.Fatalf("Expected pid to be %d, got %d\n", os.Getpid(), pid)
    44  	}
    45  }