interrupt_fs_test.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. // Copyright 2015 Google Inc. All Rights Reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package interruptfs_test
  15. import (
  16. "bytes"
  17. "os"
  18. "os/exec"
  19. "path"
  20. "testing"
  21. "time"
  22. "github.com/jacobsa/fuse/fuseutil"
  23. "github.com/jacobsa/fuse/samples"
  24. "github.com/jacobsa/fuse/samples/interruptfs"
  25. . "github.com/jacobsa/oglematchers"
  26. . "github.com/jacobsa/ogletest"
  27. )
  28. func TestInterruptFS(t *testing.T) { RunTests(t) }
  29. ////////////////////////////////////////////////////////////////////////
  30. // Boilerplate
  31. ////////////////////////////////////////////////////////////////////////
  32. type InterruptFSTest struct {
  33. samples.SampleTest
  34. fs *interruptfs.InterruptFS
  35. }
  36. func init() { RegisterTestSuite(&InterruptFSTest{}) }
  37. var _ SetUpInterface = &InterruptFSTest{}
  38. var _ TearDownInterface = &InterruptFSTest{}
  39. func (t *InterruptFSTest) SetUp(ti *TestInfo) {
  40. var err error
  41. // Create the file system.
  42. t.fs = interruptfs.New()
  43. AssertEq(nil, err)
  44. t.Server = fuseutil.NewFileSystemServer(t.fs)
  45. // Mount it.
  46. t.SampleTest.SetUp(ti)
  47. }
  48. ////////////////////////////////////////////////////////////////////////
  49. // Test functions
  50. ////////////////////////////////////////////////////////////////////////
  51. func (t *InterruptFSTest) StatFoo() {
  52. fi, err := os.Stat(path.Join(t.Dir, "foo"))
  53. AssertEq(nil, err)
  54. ExpectEq("foo", fi.Name())
  55. ExpectEq(0777, fi.Mode())
  56. ExpectFalse(fi.IsDir())
  57. }
  58. func (t *InterruptFSTest) InterruptedDuringRead() {
  59. var err error
  60. t.fs.EnableReadBlocking()
  61. // Start a sub-process that attempts to read the file.
  62. cmd := exec.Command("cat", path.Join(t.Dir, "foo"))
  63. var cmdOutput bytes.Buffer
  64. cmd.Stdout = &cmdOutput
  65. cmd.Stderr = &cmdOutput
  66. err = cmd.Start()
  67. AssertEq(nil, err)
  68. // Wait for the command in the background, writing to a channel when it is
  69. // finished.
  70. cmdErr := make(chan error)
  71. go func() {
  72. cmdErr <- cmd.Wait()
  73. }()
  74. // Wait for the read to make it to the file system.
  75. t.fs.WaitForFirstRead()
  76. // The command should be hanging on the read, and not yet have returned.
  77. select {
  78. case err = <-cmdErr:
  79. AddFailure("Command returned early with error: %v", err)
  80. AbortTest()
  81. case <-time.After(10 * time.Millisecond):
  82. }
  83. // Send SIGINT.
  84. cmd.Process.Signal(os.Interrupt)
  85. // Now the command should return, with an appropriate error.
  86. err = <-cmdErr
  87. ExpectThat(err, Error(HasSubstr("signal")))
  88. ExpectThat(err, Error(HasSubstr("interrupt")))
  89. }
  90. func (t *InterruptFSTest) InterruptedDuringFlush() {
  91. var err error
  92. t.fs.EnableFlushBlocking()
  93. // Start a sub-process that attempts to read the file.
  94. cmd := exec.Command("cat", path.Join(t.Dir, "foo"))
  95. var cmdOutput bytes.Buffer
  96. cmd.Stdout = &cmdOutput
  97. cmd.Stderr = &cmdOutput
  98. err = cmd.Start()
  99. AssertEq(nil, err)
  100. // Wait for the command in the background, writing to a channel when it is
  101. // finished.
  102. cmdErr := make(chan error)
  103. go func() {
  104. cmdErr <- cmd.Wait()
  105. }()
  106. // Wait for the flush to make it to the file system.
  107. t.fs.WaitForFirstFlush()
  108. // The command should be hanging on the flush, and not yet have returned.
  109. select {
  110. case err = <-cmdErr:
  111. AddFailure("Command returned early with error: %v", err)
  112. AbortTest()
  113. case <-time.After(10 * time.Millisecond):
  114. }
  115. // Send SIGINT.
  116. cmd.Process.Signal(os.Interrupt)
  117. // Now the command should return, with an appropriate error.
  118. err = <-cmdErr
  119. ExpectThat(err, Error(HasSubstr("signal")))
  120. ExpectThat(err, Error(HasSubstr("interrupt")))
  121. }