File: flock_test.go

package info (click to toggle)
receptor 1.5.5-2
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 2,772 kB
  • sloc: python: 1,643; makefile: 305; sh: 174
file content (93 lines) | stat: -rw-r--r-- 1,579 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
//go:build !windows
// +build !windows

package utils_test

import (
	"os"
	"path/filepath"
	"testing"

	"github.com/ansible/receptor/pkg/utils"
)

func TestTryFLock(t *testing.T) {
	type args struct {
		filename string
	}
	tests := []struct {
		name    string
		args    args
		want    *utils.FLock
		wantErr bool
	}{
		{
			name: "Positive",
			args: args{
				filename: filepath.Join(os.TempDir(), "good_flock_listener"),
			},
			want:    &utils.FLock{0},
			wantErr: false,
		},
		{
			name: "Negative",
			args: args{
				filename: "",
			},
			want:    &utils.FLock{},
			wantErr: true,
		},
	}
	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			got, err := utils.TryFLock(tt.args.filename)
			if (err != nil) != tt.wantErr {
				t.Errorf("%s: TryFLock(): error = %v, wantErr %v", tt.name, err, tt.wantErr)

				return
			}

			if err == nil {
				if got.Fd < 0 {
					t.Errorf("%s: UnixSocketListen(): Invalid got Fd = %+v", tt.name, got)
				}
			}
		})
	}
}

func TestFLock_Unlock(t *testing.T) {
	type fields struct {
		Fd int
	}
	tests := []struct {
		name    string
		fields  fields
		wantErr bool
	}{
		{
			name: "Positive",
			fields: fields{
				Fd: 1,
			},
			wantErr: false,
		},
		{
			name: "Negative",
			fields: fields{
				Fd: -1,
			},
			wantErr: true,
		},
	}
	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			lock := &utils.FLock{
				Fd: tt.fields.Fd,
			}
			if err := lock.Unlock(); (err != nil) != tt.wantErr {
				t.Errorf("%s: FLock.Unlock() error = %v, wantErr %v", tt.name, err, tt.wantErr)
			}
		})
	}
}