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

Stub check calls without order. #132

Merged
merged 3 commits into from
Jan 15, 2018

Conversation

anastasiamac
Copy link
Contributor

All current methods on testing Stub assume that the order of calls matters.

However, there are situations where we only care about the fact that the calls where made, not in what order they were made. For example, intermittent failures in https://bugs.launchpad.net/juju/+bug/1742222 specifically occur because we expect the calls to have been made but the calls in asynchronous system can be made in any order.

This PR adds a method that allows to check that the expected calls where made, not when they were made.

Copy link
Member

@jameinel jameinel left a comment

Choose a reason for hiding this comment

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

Needs a small tweak, but otherwise I think the functionality is quite useful.

stub.go Outdated
// This method explicitly does not check if the calls were made in order, just
// whether they have been made.
func (f *Stub) CheckCallsUnordered(c *gc.C, expected []StubCall) {
c.Check(len(f.calls), gc.Equals, len(expected))
Copy link
Member

Choose a reason for hiding this comment

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

This will do the wrong thing if you ask about:
[a, b, c, c]
And you actually get
[a, b, b, c]
Because in both cases the len is correct, and all of a, b, c was called.

Just doing:

calls = f.calls[:]
removeFound = func(call StubCall) bool {
  foundIdx := -1
  for idx, madeCall := range calls {
    if reflect.DeepEqual(call, madeCall) {
      foundIdx = idx
      break
    }
  }
  if foundIdx == -1 {
    return false
  }
 calls = append(calls[:foundIdx], calls[foundIdx+1]...)
  return true
}

The other nice thing about this is when you're done with the loop, you can do
c.Check(calls, gc.DeepEquals, []StubCall{})

which should print out any calls that happened that you weren't expecting.

@anastasiamac
Copy link
Contributor Author

Oh, good call \o/
I've changed your suggestion slightly for simplicity and added comments. I have also added a unit test to check this scenario.
Thank you for the review.

@anastasiamac
Copy link
Contributor Author

$$merge$$

@jujubot
Copy link
Contributor

jujubot commented Jan 10, 2018

Status: merge request accepted. Url: http://ci.jujucharms.com/job/github-merge-juju-testing

@jujubot
Copy link
Contributor

jujubot commented Jan 10, 2018

Build failed: Tests failed
build url: http://ci.jujucharms.com/job/github-merge-juju-testing/76

@anastasiamac
Copy link
Contributor Author

$$try-again$$

Failure unrelated to my change...
Fetching https://gopkg.in/mgo.v2?go-get=1

Parsing meta tags from https://gopkg.in/mgo.v2?go-get=1 (status code 200)

get "gopkg.in/mgo.v2": found meta tag get.metaImport{Prefix:"gopkg.in/mgo.v2", VCS:"git", RepoRoot:"https://gopkg.in/mgo.v2"} at https://gopkg.in/mgo.v2?go-get=1

2018-01-10 23:40:49 WARNING juju.testing mgo.go:218 failed to start mongo: exec: "/usr/local/bin/mongod": stat /usr/local/bin/mongod: no such file or directory

--- FAIL: Test (0.00s)

mgo.go:456: exec: "/usr/local/bin/mongod": stat /usr/local/bin/mongod: no such file or directory

FAIL

FAIL github.com/juju/testing 0.024s

@anastasiamac
Copy link
Contributor Author

$$merge$$

@jujubot
Copy link
Contributor

jujubot commented Jan 10, 2018

Status: merge request accepted. Url: http://ci.jujucharms.com/job/github-merge-juju-testing

@jujubot
Copy link
Contributor

jujubot commented Jan 10, 2018

Build failed: Tests failed
build url: http://ci.jujucharms.com/job/github-merge-juju-testing/77

@jujubot jujubot merged commit 799dbfe into juju:master Jan 15, 2018
@anastasiamac anastasiamac deleted the stub-check-calls-without-order branch January 15, 2018 00:57
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.

3 participants