lang: funcs: Add a new test for readfile and fix a small bug

This adds a new test for readfile. Interestingly, it actually caught a
small bug, which was also fixed with this commit. I think the bug was
actually always masked, because it only occurred on shutdown, and in
this case we often don't care about how the stream exited, but it's a
good example of how a test case focused on just one small aspect can be
important.

As an aside, this test case also would have caught the bug fixed in
94c40909cc and by reverting that patch it
indeed fails.
This commit is contained in:
James Shubin
2019-03-05 11:34:31 -05:00
parent de1691665f
commit 11fc55d679
2 changed files with 38 additions and 1 deletions

View File

@@ -454,7 +454,43 @@ func TestLiveFuncExec0(t *testing.T) {
cleanup: func() error { return nil }, cleanup: func() error { return nil },
}) })
} }
{
p := "/tmp/somefiletoread"
content := "hello world!\n"
timeline := []Step{
NewSendInputs([]types.Value{
vog(p),
}),
NewWaitForNValues(1, timeout), // more than 1 blocks here
NewWaitForNSeconds(5, 10), // wait longer just to be sure
// pass in a custom validation function
NewRangeExpect(func(args []types.Value) error {
//fmt.Printf("range: %+v\n", args) // debugging
if c := len(args); c != 1 {
return fmt.Errorf("wrong args count, got: %d", c)
}
if args[0].Type().Kind != types.KindStr {
return fmt.Errorf("expected str, got: %+v", args[0])
}
if err := vog(content).Cmp(args[0]); err != nil {
return errwrap.Wrapf(err, "got different expected value: %+v", args[0])
}
return nil
}),
}
testCases = append(testCases, test{
name: "readfile",
hostname: "", // not needed for this func
funcname: "os.readfile",
timeline: timeline,
expect: func() error { return nil },
startup: func() error { return ioutil.WriteFile(p, []byte(content), 0666) },
cleanup: func() error { return os.Remove(p) },
})
}
names := []string{} names := []string{}
for index, tc := range testCases { // run all the tests for index, tc := range testCases { // run all the tests
if tc.name == "" { if tc.name == "" {

View File

@@ -206,7 +206,8 @@ func (obj *ReadFileFunc) Stream() error {
// Close runs some shutdown code for this function and turns off the stream. // Close runs some shutdown code for this function and turns off the stream.
func (obj *ReadFileFunc) Close() error { func (obj *ReadFileFunc) Close() error {
close(obj.events) // clean up for fun
close(obj.closeChan) close(obj.closeChan)
obj.wg.Wait() // block so we don't exit by the closure of obj.events
close(obj.events) // clean up for fun
return nil return nil
} }