@nathankerr, following your suggestion I went with runtime.GoSched()
. This made sense to me, so I implemented it in my test. The first check (as described above) passes, but the second does not. Coincidentally, this is the same behaviour as when I debug.
I decided to write a smaller test to validate my understanding:
func TestAuction_AnnounceClosed(t *testing.T) {
a := &auction{}
a.AnnounceClosed()
expected := "CLOSED"
if actual := a.GetStatus(); actual != expected {
t.Errorf("Expected; %v, got; %v", actual, expected)
}
actual := reflect.ValueOf(bidding(a))
expected = reflect.ValueOf(lost)
if actual.Pointer() != expected.Pointer() {
t.Errorf("Expected; %v, got; %v", actual, expected)
}
}
In the second if statement I’m checking that the identity of the function returned from bidding()
is the lost
function when status
is set to CLOSED
. I have done this with winning
function successfully, but the compiler does not like lost
. I get the error:
auction/state_test.go:48: cannot use reflect.ValueOf(lost) (type reflect.Value) as type string in assignment
auction/state_test.go:51: expected.Pointer undefined (type string has no field or method Pointer)
The following test for winning
passes without a hitch:
func TestAuction_BidPriceGreaterThanMaxBid(t *testing.T) {
a := &auction{
maxBid: 100,
bidPrice: 100,
}
actual := reflect.ValueOf(bidding(a))
expected := reflect.ValueOf(winning)
if actual.Pointer() != expected.Pointer() {
t.Errorf("Expected; %v, got; %v", actual, expected)
}
}
winning
and lost
are implemented as follows:
func winning(a *auction) stateFn {
return nil
}
func lost(a *auction) stateFn {
a.mu.Lock()
defer a.mu.Unlock()
a.Status = "LOST"
return nil
}
… and for completeness, here is the bidding()
function:
func bidding(a *auction) stateFn {
a.mu.Lock()
defer a.mu.Unlock()
if a.Status == "CLOSED" {
return lost
}
a.Status = "BIDDING"
if a.bidPrice <= a.maxBid {
return winning
}
return bidding
}
I am now completely bamboozled as to why lost
isn’t behaving the same as winning
, but I suspect it has everything to do with why the main test isn’t passing. Anything obvious? could this be a bug in Go?