A framework for concurrent jdbc unit tests.

The class ConcurrentCommandGenerator providea a java API for constructing concurrent jdbc tests: an instance of the class represents a test case, which contains several sequences of SQL commands (abstracted as subclasses of ConcurrentCommand). Each sequence is run in its own thread as a separate jdbc client (ie a separate java.sql.Connection). There are facilties to synchronize these command threads. Only a simple command sequence is supported: no branching, no looping.

An alternative is to define a test by writing a test script in mtsql format, as described below. An instance of ConcurrentCommandScript parses and executes a script.
Revision $Id$
Copyright Copyright (C) 2005 The Eigenbase Project
Copyright (C) 2005 SQLstream, Inc.
Copyright (C) 2005 Dynamo BI Corporation
Author Stephan Zuercher


Script Format

Syntax:

The syntactic structure of an mtsql script is:

    <directive>*
    <setup section>?
    <cleanup section>?
    <thread section>+

    <directive>         := @[no]lockstep | @enable | @disable
    <setup section>     := @setup <basic command>* @end 
    <cleanup section>   := @setup <basic command>* @end 
    <thread section>    := @thread <thread-name>? <command>* @end

    <command> := 
      <basic command> | 
      <command prefix>? <threadly command> | 
      <synchronization point>

Blank lines and comments are allowed anywhere. A comment starts with two hyphens and runs to the end of the line. Command names start with an '@'. Some commands run to the end of the line; but a command that contains SQL can span lines and ends with a semicolon.

Semantics:

Running a section means running its commands in sequence. First the setup section (if any) is run. Next all the thread sections are run at once, each in its own thread. When all these threads complete, the cleanup section (if any) is run.

Synchronization:

The threads are synchronized by inserting synchronization points (@sync).

When a thread reaches a @sync, it waits until all threads are waiting on the same @sync: then all threads proceed. @sync points have no names. Clearly all thread sections must contain the same number of @sync points.

The directive @lockstep has the same effect as adding a @sync after each command in every thread section. Clearly it requires that all thread sections have the same number of commands. The default is the antonym @nolockstep.

The directive @disable means "skip this script". The deault is the antonym @enable.

Error handling:

When a sql command fails, the rest of its section is skipped. However, if the attribute force is true the error is ignored, and the section continues. force has an independent value in each section. Within a section it can be toggled using the sql directive !SET FORCE val, where val can be true, false, on, off. (This is modelled after sqlline and sqllineClient. Other sqlline !-directives are ignored.)

An error in a thread section will stop that thread, but the other threads continue (with one fewer partner to synchronize with), and finally the cleanup section runs. If the setup section quits, then only the cleanup section is run.

Basic Commands (allowed in any section):

<SQL statement>;
  An SQL statement terminated by a semicolon. The statement can span lines.

@include FILE 
  Reads and executes the contents of FILE, another mtsql script.
  Inclusions may nest.

Threaded Commands (allowed only in a @thread section):

@sleep N        -- thread sleeps for N milliseconds
@echo MESSAGE   -- prints the message to stdout

<SQL statement> ';'             -- executes the SQL
@timeout N <SQL>  ';'    -- executes the SQL with the given timeout (msecs)
@rowlimit N <SQL> ';'    -- executes the SQL, stops fetching after N rows.
@err <SQL> ';'           -- executes the SQL, expecting it to fail.

@repeat N <command>+ @end
    Denotes a repeated block of commands, with repeat count = N.
    N must be positive. 
                
@prepare SQL-STATEMENT ';' 
    Prepares the sql. A thread has at most one prepared statement at a time.

@print FORMAT
    Sets the result-printing format for the current prepared statement.
    FORMAT is a sequence of the phrases:
        none             -- means print nothing
        all              -- means print all rows (the default)
        every N          -- means print the first row (row 0), then row N, 2N, etc.
        count            -- means print row number (starts with 0).
        time             -- means print the time each printed row was fetched.
        total            -- means print a final summary, 
                            with row count and net fetch time (not including any timeout).
   (Sorry, no way yet to print selected columns, to print time in a special way, etc.)


@fetch <timeout>?
    Starts fetching and printing result rows, with an optional timeout (in msecs).
    Stop on EOD or on timeout.

@close
    Closes the current prepared statement. However that an open prepared
    statement will be closed automatically at the end of its thread.

@shell <Shell Command>
    Runs the command in a spawned subshell, proceeds after it concludes, but quits if it fails.
    For @shell and @echo, the command or message runs to the end of the line in the script, 
    but can be continued if the line ends with a single '\'.

Substituted Variables

Needed mainly to pass arguments to the command of @shell, but also useful to parameterize SQL statements, timeout values etc.