Serial Number Generator

LegacyForumLegacyForum Posts: 1,669 ✭✭
edited December 2016 in SOAtest
How to generate a series of positive integers?
Howdy folks,

I'm writing soatest-cases for a set-of-webservices which implement a "logical conversation". I need to pass a unique conversationId (as a soap-header parameter) with each service request. At the moment I'm just choosing a random number between 1 and 10,000,000 for each scenario... but (now I'm upto over a hundred scenarios) finding & resolving the inevitable collisions is getting UGLY!

Please could anyone advise on How might I go about writing a serial number generator? I'm thinking of a python (or java) method which returns 1 the first time it is called, 2 the second time, 3 the third, and so on upto (2^31)-1... within an execution of my test-suit. Alternately, maybe the number of milliseconds since midnight (0..86,400,000) might be simpler? and do the trick just as well? Concurrency/contention is NOT an issue I think as the test-cases will allways be run synchronously, and by a single user at any one time.

I was thinking I'd save my serial-number in a soatest-variable, and then use it's value in the parameterised soap-header-parameter of each request within a scenario. Where/how could I call the serial-number-generator script at the start of each scenario? Is there a scenario-init method I can hijack, or would I need to manually retrofit every scenario with an initialisation-test-case.

I would appreciate any pointers in right direction. I shall undertake some heavy googling in meantime.

Cheers all, Keith.

Tagged:

Comments

  • LegacyForumLegacyForum Posts: 1,669 ✭✭
    edited December 2008
    Hi corlettk,

    I recently had to create a Jython script which generated a two or three char key. (I probably hvae a post out here somewhere asking for help as well!)
    I retrofitted it to generate a random 10 character key comprised of letters and numbers. So far, it has generated U1AS2CQXKA, 515S7VL5RE, and R13QWEYPS1. I have no idea what the chances are that it would generate the same key twice, but I'd bet it's pretty slim.

    I have attached it for you. Here are the bits you'll need to know when using it:

    * It is Jython code.

    * If you want to add lower case letters, add them to the line -> chars = String("ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789")

    * For each char you add, you'll need to increase the line -> i = int(Math.floor(Math.random() * 36))
    by 1 so that the search through the string goes all the way to the end.

    * To see it work, just run the "Test 1" step and check the return in the "Return Value->XML Data Bank (see the Literal tab)".

    I'll keep working with it, but for now it just generates a single key. We might be able to change it to generate more keys, then you could do this:

    Generate 100 keys
    Drop them into XML Data bank
    Be accessed later in your test suite

    Enjoy!

    BrianH
  • LegacyForumLegacyForum Posts: 1,669 ✭✭

    I recently had to create a Jython script which generated a two or three char key...

    Thanx Brian,

    I just need integers, but your idea of a datasource got me thinking... What I'm going to try is a java class which is-a datasource... It'll just create-or-rename a directory ./MySoaTestFilename.serialNumber.1 (for persistence) and return the next number ... Sounds easy enough.

    I really appreciate you volunteering your thoughts. iouBeer++

    Cheers. Keith.
  • LegacyForumLegacyForum Posts: 1,669 ✭✭
    If you're only concerned about generating positive integers, whereas the value of the integer doesn't matter, then I would recommend to simply grab the current time in milliseconds (e.g. System.currentTimeMillis() returns the current time in long ms - FYI, our python scripting is really Jython which is a hybrid of Java and Python, this means you can use Java objects in your python/jython script). This will always be unique, but the value may be too large considering it's actually of type long. At any rate, scripting would be the most ideal choice to accomplish what you're doing.

    I recently had to create a Jython script which generated a two or three char key...

    Thanx Brian,

    I just need integers, but your idea of a datasource got me thinking... What I'm going to try is a java class which is-a datasource... It'll just create-or-rename a directory ./MySoaTestFilename.serialNumber.1 (for persistence) and return the next number ... Sounds easy enough.

    I really appreciate you volunteering your thoughts. iouBeer++

    Cheers. Keith.
  • LegacyForumLegacyForum Posts: 1,669 ✭✭

    If you're only concerned about generating positive integers...

    truong,

    I reckon I'll just use System.currentTimeMillis() % 1,000,000,000 which is int-safe and surely must be distinct enough (1,000,000,000 / (1,000 * 60 * 60 * 24) = 11.5740741).

    I hadn't realised that SoaTests-Python is actually Jython. Cool as!!!

    Thank you.

    Cheers, Keith.
  • LegacyForumLegacyForum Posts: 1,669 ✭✭
    Yup...

    Method: generateConversationId
    CODE

    from soaptest.api import * # Required for SOAPUtil
    from java.lang import * # Required for System, String, Math, etc,

    def generateConversationId(context):
    return SOAPUtil.getXMLFromString([str(System.currentTimeMillis() % (1000*1000*1000))])

    --> XML Data Bank (just rename the default z0 to conversationId)

    --> Works like a charm -->

    Thank you gentlemen... It's been fun.
Sign In or Register to comment.