Пример #1
0
func (gen *myGenerator) asyncCall() {
	gen.tickets.Take()
	go func() {
		defer func() {
			if p := recover(); p != nil {
				err, ok := interface{}(p).(error)
				var buff bytes.Buffer
				buff.WriteString("Async Call Panic! (")
				if ok {
					buff.WriteString("error: ")
					buff.WriteString(err.Error())
				} else {
					buff.WriteString("clue: ")
					buff.WriteString(fmt.Sprintf("%v", p))
				}
				buff.WriteString(")")
				errMsg := buff.String()
				logger.Fatalln(errMsg)
				result := &lib.CallResult{
					Id:   -1,
					Code: lib.RESULT_CODE_FATAL_CALL,
					Msg:  errMsg}
				gen.sendResult(result)
			}
		}()
		rawReq := gen.caller.BuildReq()
		var timeout bool
		timer := time.AfterFunc(gen.timeoutNs, func() {
			timeout = true
			result := &lib.CallResult{
				Id:   rawReq.Id,
				Req:  rawReq,
				Code: lib.RESULT_CODE_WARNING_CALL_TIMEOUT,
				Msg:  fmt.Sprintf("Timeout! (expected: < %v)", gen.timeoutNs)}
			gen.sendResult(result)
		})
		rawResp := gen.interact(&rawReq)
		if !timeout {
			timer.Stop()
			var result *lib.CallResult
			if rawResp.Err != nil {
				result = &lib.CallResult{
					Id:     rawResp.Id,
					Req:    rawReq,
					Code:   lib.RESULT_CODE_ERROR_CALL,
					Msg:    rawResp.Err.Error(),
					Elapse: rawResp.Elapse}
			} else {
				result = gen.caller.CheckResp(rawReq, *rawResp)
				result.Elapse = rawResp.Elapse
			}
			gen.sendResult(result)
		}
		gen.tickets.Return()
	}()
}
Пример #2
0
func (comm *TcpComm) CheckResp(
	rawReq loadgenlib.RawReq, rawResp loadgenlib.RawResp) *loadgenlib.CallResult {
	var commResult loadgenlib.CallResult
	commResult.Id = rawResp.Id
	commResult.Req = rawReq
	commResult.Resp = rawResp
	var sreq ServerReq
	err := json.Unmarshal(rawReq.Req, &sreq)
	if err != nil {
		commResult.Code = loadgenlib.RESULT_CODE_FATAL_CALL
		commResult.Msg =
			fmt.Sprintf("Incorrectly formatted Req: %s!\n", string(rawReq.Req))
		return &commResult
	}
	var sresp ServerResp
	err = json.Unmarshal(rawResp.Resp, &sresp)
	if err != nil {
		commResult.Code = loadgenlib.RESULT_CODE_ERROR_RESPONSE
		commResult.Msg =
			fmt.Sprintf("Incorrectly formatted Resp: %s!\n", string(rawResp.Resp))
		return &commResult
	}
	if sresp.Id != sreq.Id {
		commResult.Code = loadgenlib.RESULT_CODE_ERROR_RESPONSE
		commResult.Msg =
			fmt.Sprintf("Inconsistent raw id! (%d != %d)\n", rawReq.Id, rawResp.Id)
		return &commResult
	}
	if sresp.Err != nil {
		commResult.Code = loadgenlib.RESULT_CODE_ERROR_CALEE
		commResult.Msg =
			fmt.Sprintf("Abnormal server: %s!\n", sresp.Err)
		return &commResult
	}
	if sresp.Result != op(sreq.Operands, sreq.Operator) {
		commResult.Code = loadgenlib.RESULT_CODE_ERROR_RESPONSE
		commResult.Msg =
			fmt.Sprintf(
				"Incorrect result: %s!\n",
				genFormula(sreq.Operands, sreq.Operator, sresp.Result, false))
		return &commResult
	}
	commResult.Code = loadgenlib.RESULT_CODE_SUCCESS
	commResult.Msg = fmt.Sprintf("Success. (%s)", sresp.Formula)
	return &commResult
}