Ted
Ted

Reputation: 395

Unexpected type-error python

I am trying to create a client used to mainly test out the responses of a server asynchronously. I have created a function that basically waits for the next response from the server, if a requestId is provided when this function is called it will look for the next response with the requestId provided. Here is the function:

def getNextResponse(self, requestId = None):
    logger = logging.getLogger(__name__)
    self.acknowledge += 1
    logger.info("requestId ack for this response: {}".format(requestId))
    while(not self.response):
        pass
    self.acknowledge -= 1
    logger.info("requestId unset for this response: {}".format(requestId))
    message = json.loads(self.messagesList[len(self.messagesList)-1])

    if(requestId != None):
        while(requestId != message['body']['requestId']):
            self.acknowledge += 1
            while(not self.response):
                pass    
            self.acknowledge -= 1
            message = self.messagesList[len(self.messagesList)-1]

    self.startMonitor -= 1       
    return message['body']

I also have helper functions for each command which can be sent to the engine below is one of said helper function for a ping command:

 def ping(self, sessionId = None, requestId = None, version="1.0"):
    result = {
        "method": "Ping"
        }
    if(None != version):
        result['version'] = version
    if(None != sessionId):
        result['sessionId'] = sessionId
    if(None != requestId):
        result['requestId'] = requestId
    logger = logging.getLogger(__name__)
    logger.info("Message Sent: " + json.dumps(result, indent=4))
    self.startMonitor += 1
    self.ws.send(json.dumps(result))
    message = self.getNextResponse(requestId = requestId)

    return message

It basically sets up a json object which contains all the parameters that the server expects and then sends the entire json message to the server. After it has been sent i call getNextResponse to await a response from the server. The requestId is set to None by default, so if no requestId is provided, it will just look for the very next response returned by the server. Since this can be quite inconsistent because of other asynchronous commands, one can provided a unique requestId for the command so that the response from the server will also contain this requestId thus making each response unique.

In my test case I am generating a random requestId by using:

def genRequestId(self):
    x = random.randint(10000000, 99999999)
    print x
    reqId = str(x)+"-97a2-11e6-9346-fde5d2234523"
    return reqId  

The problem that I encountered is that sometimes (seems to be random), when I call ping in one of my test cases, i get this error:

message = self.getNextResponse(requestId = requestId)
TypeError: string indices must be integers, not str

I am quite confused by this error, requestId that I am generating inside ping is supposed to be a string and I am not referencing inside it in any way. I have tried removing the reference to the parameter like so:

message = self.getNextResponse(requestId)

But I am still getting this error. The error doesn't go any deeper inside the getNextResponse function which leads me to believe that it is coming from inside the ping function when I try to call it. Any help would be greatly appreciated!

EDIT: Here is the error

Traceback (most recent call last): File "ctetest./RegressionTest/WebsocketTest\test_18_RecTimer.py", line 385, in test009_recTimer_Start_withAudio response = client.endSession(sessionId = sessionId, requestId = requestId_2)

File "ctetest./RegressionTest/WebsocketTest../.././CTESetupClass\WebsocketCl ient.py", line 528, in endSession message = self.getNextResponse(requestId) File "ctetest./RegressionTest/WebsocketTest../.././CTESetupClass\WebsocketCl ient.py", line 49, in wrapper raise ret TypeError: string indices must be integers, not str

Upvotes: 0

Views: 4406

Answers (1)

Tadhg McDonald-Jensen
Tadhg McDonald-Jensen

Reputation: 21464

you have two statements in your code that look very similar:

message = json.loads(self.messagesList[len(self.messagesList)-1])

and then further down:

        message = self.messagesList[len(self.messagesList)-1]

The first will set message to a json object (probably dict) where the second one assigns message to a string, I'm assuming this is not intended and the cause for your error.

Upvotes: 1

Related Questions