Go app hangs when testing a function that contains a lock -
this function wrote adds request request queue:
func (self *requestqueue) addrequest(request *request) { self.requestlock.lock() self.queue[request.normalizedurl()] = request.responsechannel self.requestlock.unlock() }
and 1 of tests:
func testaddrequest(t *testing.t) { before := len(rq.queue) r := samplerequests(1)[0] rq.addrequest(&r) if (len(rq.queue) - 1) != before { t.errorf("failed add request queue") } }
when run test, application hangs. if comment out test, works fine. think problem locking inside function. there i'm doing wrong? help!
the problem infinite loop in samplerequests() function:
func samplerequests(num int) []request { requests := make([]request, num, num+10) := 0; < len(requests); i++ { r := newrequest("get", "http://api.openweathermap.org/data/2.5/weather", nil) r.params.set("lat", "35") r.params.add("lon", "139") r.params.add("units", "metric") requests = append(requests, r) } return requests }
i checking if i
less length of array in continuation condition of loop. each iteration, item added array, length increased , loop continued executing.
Comments
Post a Comment