golang / mock

GoMock is a mocking framework for the Go programming language.
Apache License 2.0
9.3k stars 610 forks source link

Reference a call into a variable #532

Open sirkon opened 3 years ago

sirkon commented 3 years ago

Requested feature

A function to make a reference of a call into a call variable:

func Ref(dest **gomock.Call, call *gomock.Call) *gomock.Call {
    *dest = call
    return call
}

This func just assigns a call to a call variable.

Why this feature is needed

In easiest cases gomock.InOrder is more than enough to describe an order of calls. But once you spawn a goroutine it becomes a partial one. Consider an scheme where:

Parent thread makes calls calls Ax, spawn goroutines and then do some calls B1, …,Bm: A1 A2 An spawn goroutines B1 Bm

With such a reference helper you can do just several well-separated gomock.InOrder calls to ensure a proper order:

var anCall *gomock.Call

// order for the main thread
gomock.InOrder(A1, A2, …, gomock.Ref(&anCall, An), B1, …, Bm)

// orders for goroutines
gomock.InOrder(g11.After(anCall), g12, g13, …)
gomock.InOrder(g21.After(anCall), g22, g23, …)
…
sirkon commented 3 years ago

BTW, it would be less needed, albeit not totally useless, if gomock.InOrder would return its last call as requested in https://github.com/golang/mock/issues/403