Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rewrite tests #478

Merged
merged 4 commits into from Jul 31, 2022
Merged

Rewrite tests #478

merged 4 commits into from Jul 31, 2022

Conversation

arp242
Copy link
Member

@arp242 arp242 commented Jul 30, 2022

This rewrites quite a lot of tests to be much more easily readable.

While working on #472 I wanted to check "how do renames behave now?",
and found this quite hard as most test cases were >90% "plumbing", and
seeing "what file operations does this do?" and "what events do we get?"
was not very easy.

So refactor the lot, based on some work I did in #472:

  • Add a bunch of "shell-like" helper functions so you're not forever
    typing error checks, filepath.Join(), and eventSeparator(). Just
    touch(t, tmp, "file") will create a file in tmp.

  • Add eventCollector type which will collect all events in in a slice,
    replacing the previous "counter". This also ensures that the Watcher
    is closed within a second (this removes a lot of duplicate code).

    This is also much more precise than merely counting events; before
    random events could get emitted but if you weren't counting those then
    you'd never know.

    Downside is that some tests are a bit more flaky now as some
    behaviours are not always consistent in various edge cases; these are
    pre-existing bugs.

  • Add Events (plural) type (only for tests), and helper function to
    create this from a string like:

    REMOVE  /link
    CREATE  /link
    WRITE   /link
    

    Which makes seeing which events are received, diffing them, etc. much
    easier.

  • Add Parallel() for most tests; reduces runtime on my system from ~12
    seconds to ~6 seconds.

All in all it reduces the integrations_test.go from 1279 lines to 405
lines and it's quite easy to see which events are expected for which
operations, which should make things a lot easier going forward.

This rewrites quite a lot of tests to be much more easily readable.

While working on #472 I wanted to check "how do renames behave now?",
and found this quite hard as most test cases were >90% "plumbing", and
seeing "what file operations does this do?" and "what events do we get?"
was not very easy.

So refactor the lot, based on some work I did in #472:

- Add a bunch of "shell-like" helper functions so you're not forever
  typing error checks, filepath.Join(), and eventSeparator(). Just
  touch(t, tmp, "file") will create a file in tmp.

- Add eventCollector type which will collect all events in in a slice,
  replacing the previous "counter". This also ensures that the Watcher
  is closed within a second (this removes a lot of duplicate code).

  This is also much more precise than merely counting events; before
  random events could get emitted but if you weren't counting those then
  you'd never know.

  Downside is that some tests are a bit more flaky now as some
  behaviours are not always consistent in various edge cases; these are
  pre-existing bugs.

- Add Events (plural) type (only for tests), and helper function to
  create this from a string like:

      REMOVE  /link
      CREATE  /link
      WRITE   /link

  Which makes seeing which events are received, diffing them, etc. much
  easier.

- Add Parallel() for most tests; reduces runtime on my system from ~12
  seconds to ~6 seconds.

All in all it reduces the integrations_test.go from 1279 lines to 405
lines and it's quite easy to see which events are expected for which
operations, which should make things a lot easier going forward.
@arp242
Copy link
Member Author

arp242 commented Jul 30, 2022

This PR changes ... a lot. It's probably easiest to look at the new versions of integration_test.go and helpers_test.go instead of the diff:

https://github.com/fsnotify/fsnotify/blob/a9c46d1aeb606084031b22e27e03f5c5296fbbcd/integration_test.go

https://github.com/fsnotify/fsnotify/blob/a9c46d1aeb606084031b22e27e03f5c5296fbbcd/helpers_test.go

@arp242 arp242 requested a review from a team July 30, 2022 17:09
bep
bep previously approved these changes Jul 30, 2022
helpers_test.go Outdated
if i > 0 {
b.WriteString("\n")
}
fmt.Fprintf(b, "%-20s %q", ee.Op.String(), strings.ReplaceAll(ee.Name, `\`, "/"))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

filepath.ToSlash ?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah cheers, I didn't know about that function. Updated it.

@mattn
Copy link
Member

mattn commented Jul 30, 2022

In some tests, it appears that goroutine is not waiting for termination. This can be caused unexpected behavior when running a series of tests, which can be difficult to figure out. I don't have strong opinion but it have better to be fixed, I think in this or another pull-request. Thanks.

@arp242
Copy link
Member Author

arp242 commented Jul 30, 2022

In some tests, it appears that goroutine is not waiting for termination. This can be caused unexpected behavior when running a series of tests, which can be difficult to figure out. I don't have strong opinion but it have better to be fixed, I think in this or another pull-request. Thanks.

Which tests specifically do you mean? I think all of those that use the new eventCollector should be fine(?) But I didn't update all the tests to use it yet, so maybe some older ones don't.

@mattn
Copy link
Member

mattn commented Jul 31, 2022

Which tests specifically do you mean? I think all of those that use the new eventCollector should be fine(?) But I didn't update all the tests top use it yet, so maybe some older ones don't.

Sorry, I couldn't find the tests except two.

func (w *eventCollector) collect(t *testing.T) and func (w *eventCollector) stop(t *testing.T) Events

@arp242
Copy link
Member Author

arp242 commented Jul 31, 2022

My apologies @mattn, but I'm really confused what you mean 😅 You're saying the bug is in the eventCollector? I looked over it again, and I'm not sure if I see the problem, but I'm probably missing something.

I'll go ahead and merge this for now, so I can get ahead with some other things and add test cases and such; but let me know and I'll fix it up!

@arp242 arp242 merged commit 87dc1fa into main Jul 31, 2022
@arp242 arp242 deleted the test branch July 31, 2022 18:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants