github.com/cloudwego/hertz@v0.9.3/pkg/network/utils_test.go (about)

     1  /*
     2   * Copyright 2022 CloudWeGo Authors
     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  
    17  package network
    18  
    19  import (
    20  	"os"
    21  	"runtime"
    22  	"testing"
    23  )
    24  
    25  func TestUnlinkUdsFile(t *testing.T) {
    26  	if runtime.GOOS == "windows" {
    27  		t.SkipNow()
    28  	}
    29  
    30  	tmp := "tmpFile"
    31  	var err error
    32  
    33  	err = UnlinkUdsFile("unix", tmp)
    34  
    35  	if err == nil {
    36  		t.Errorf("should have error when unlinking a nonexistent file")
    37  	}
    38  
    39  	os.Create(tmp)
    40  
    41  	err = UnlinkUdsFile("unix", tmp)
    42  	if err != nil {
    43  		t.Errorf("unlink file failed: %s", err.Error())
    44  	}
    45  
    46  	isExist, _ := pathExists(tmp)
    47  
    48  	if isExist {
    49  		t.Errorf("unlink file failed, file still exist")
    50  	}
    51  }
    52  
    53  func pathExists(path string) (bool, error) {
    54  	_, err := os.Stat(path)
    55  	if err == nil {
    56  		return true, nil
    57  	}
    58  	if os.IsNotExist(err) {
    59  		return false, nil
    60  	}
    61  	return false, err
    62  }