File: interrupt_fs_test.go

package info (click to toggle)
golang-github-jacobsa-fuse 0.0~git20150806.0.9a7512a-4
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, buster, trixie
  • size: 584 kB
  • sloc: asm: 11; makefile: 4
file content (156 lines) | stat: -rw-r--r-- 3,945 bytes parent folder | download | duplicates (3)
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
// Copyright 2015 Google Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package interruptfs_test

import (
	"bytes"
	"os"
	"os/exec"
	"path"
	"testing"
	"time"

	"github.com/jacobsa/fuse/fuseutil"
	"github.com/jacobsa/fuse/samples"
	"github.com/jacobsa/fuse/samples/interruptfs"
	. "github.com/jacobsa/oglematchers"
	. "github.com/jacobsa/ogletest"
)

func TestInterruptFS(t *testing.T) { RunTests(t) }

////////////////////////////////////////////////////////////////////////
// Boilerplate
////////////////////////////////////////////////////////////////////////

type InterruptFSTest struct {
	samples.SampleTest
	fs *interruptfs.InterruptFS
}

func init() { RegisterTestSuite(&InterruptFSTest{}) }

var _ SetUpInterface = &InterruptFSTest{}
var _ TearDownInterface = &InterruptFSTest{}

func (t *InterruptFSTest) SetUp(ti *TestInfo) {
	var err error

	// Create the file system.
	t.fs = interruptfs.New()
	AssertEq(nil, err)

	t.Server = fuseutil.NewFileSystemServer(t.fs)

	// Mount it.
	t.SampleTest.SetUp(ti)
}

////////////////////////////////////////////////////////////////////////
// Test functions
////////////////////////////////////////////////////////////////////////

func (t *InterruptFSTest) StatFoo() {
	fi, err := os.Stat(path.Join(t.Dir, "foo"))
	AssertEq(nil, err)

	ExpectEq("foo", fi.Name())
	ExpectEq(0777, fi.Mode())
	ExpectFalse(fi.IsDir())
}

func (t *InterruptFSTest) InterruptedDuringRead() {
	var err error
	t.fs.EnableReadBlocking()

	// Start a sub-process that attempts to read the file.
	cmd := exec.Command("cat", path.Join(t.Dir, "foo"))

	var cmdOutput bytes.Buffer
	cmd.Stdout = &cmdOutput
	cmd.Stderr = &cmdOutput

	err = cmd.Start()
	AssertEq(nil, err)

	// Wait for the command in the background, writing to a channel when it is
	// finished.
	cmdErr := make(chan error)
	go func() {
		cmdErr <- cmd.Wait()
	}()

	// Wait for the read to make it to the file system.
	t.fs.WaitForFirstRead()

	// The command should be hanging on the read, and not yet have returned.
	select {
	case err = <-cmdErr:
		AddFailure("Command returned early with error: %v", err)
		AbortTest()

	case <-time.After(10 * time.Millisecond):
	}

	// Send SIGINT.
	cmd.Process.Signal(os.Interrupt)

	// Now the command should return, with an appropriate error.
	err = <-cmdErr
	ExpectThat(err, Error(HasSubstr("signal")))
	ExpectThat(err, Error(HasSubstr("interrupt")))
}

func (t *InterruptFSTest) InterruptedDuringFlush() {
	var err error
	t.fs.EnableFlushBlocking()

	// Start a sub-process that attempts to read the file.
	cmd := exec.Command("cat", path.Join(t.Dir, "foo"))

	var cmdOutput bytes.Buffer
	cmd.Stdout = &cmdOutput
	cmd.Stderr = &cmdOutput

	err = cmd.Start()
	AssertEq(nil, err)

	// Wait for the command in the background, writing to a channel when it is
	// finished.
	cmdErr := make(chan error)
	go func() {
		cmdErr <- cmd.Wait()
	}()

	// Wait for the flush to make it to the file system.
	t.fs.WaitForFirstFlush()

	// The command should be hanging on the flush, and not yet have returned.
	select {
	case err = <-cmdErr:
		AddFailure("Command returned early with error: %v", err)
		AbortTest()

	case <-time.After(10 * time.Millisecond):
	}

	// Send SIGINT.
	cmd.Process.Signal(os.Interrupt)

	// Now the command should return, with an appropriate error.
	err = <-cmdErr
	ExpectThat(err, Error(HasSubstr("signal")))
	ExpectThat(err, Error(HasSubstr("interrupt")))
}