[bank-account] potential BUG

local go version: go1.20.6 linux/amd64
exercise: Bank Account in Go

I may have found an infrastructure bug.
My tests timed out on the server, but couldn’t find where I was messing up even when comparing to community solutions. I requested mentoring, and bernot-dev replied and mentioned that he thinks it’s an issue with infrastructure. We both run it and pass tests locally.

Let me know what other information I may provide.

Unsure if this will give you access, Direct mentoring link to my attempt on this exercise, with source copied below.

my code, copied from iteration 5

EDIT: I initially didn’t include all the code by accident

package account

import (
	"sync"
)

// Define the Account type here.
type Account struct {
	sync.RWMutex
	balance int64
	active  bool
}

func Open(amount int64) *Account {
	if amount < 0 {
		return nil
	}

	return &Account{
		balance: amount,
		active:  true,
	}

}

// Balance returns the balance of the account
func (a *Account) Balance() (int64, bool) {
	a.RLock()
	defer a.RUnlock()

	// fail early in lieu of returning invalid state on API
	if a.balance > 0 && !a.active {
		panic("an inactive account has nonzero balance")
	} else if a.balance < 0 {
		panic("account found with negative balance")
	}

	return a.balance, a.active
}

func (a *Account) Deposit(amount int64) (int64, bool) {
	a.Lock()
	defer a.Unlock()

	if !a.active || a.balance+amount < 0 {
		return a.balance, false
	} else {
		a.balance += amount
		return a.balance, true
	}
}

func (a *Account) Close() (int64, bool) {
	a.Lock()
	defer func() {
		if !a.active {
			a.balance = 0
		}
		a.Unlock()
	}()

	if !a.active {
		return a.balance, false
	} else {
		a.active = false
		return a.balance, true
	}
}

Hello :slight_smile:

I presume that’s not your full file as there’s an uneven amount of brackets. Could you update it to the full iteration please?

@junedev @andrerfcsantos Any ideas?

Hey!

From the code you are showing, there’s a missng bracket at the end of the Open function. Not sure if it’s missing from your iteration too or you just didn’t copy it.

Also note that the Open function returns an *Account, but you are not returning anything in the last return.

Sorry that took a bit, not sure what happened the first time.

It seems the bug is still present: my code ran into the same timeout issue so I put back panic() in all the functions, with the same result.