TestContext

public class TestContext<T extends ComponentDefinition>
Author:

Ifeanyi Ubah

Parameters:
  • <T> – the ComponentDefinition to test

Fields

logger

static final Logger logger

timeout

public static final long timeout

Timeout value in milliseconds - Default 400ms

Methods

allow

public <P extends PortType> TestContext<T> allow(KompicsEvent event, Port<P> port, Direction direction)

Whitelist the specified event within the current block. This means that if an event matching the specified one occurs at any position within the block then that event is handled normally. However such events are not necessary for a successful testcase.

Modes - HEADER.

Parameters:
  • event – event to be matched against.
  • port – port on which the matched event should occur.
  • direction – Direction IN or OUT of the event.
  • <P> – port type.
Returns:

current TestContext

allow

public <E extends KompicsEvent, P extends PortType> TestContext<T> allow(Class<E> eventType, Predicate<E> predicate, Port<P> port, Direction direction)

Similar to allow(KompicsEvent,Port,Direction). Matches events only if predicate returns true instead.

allow

public <E extends KompicsEvent, P extends PortType> TestContext<T> allow(Class<E> eventType, Port<P> port, Direction direction)

Similar to allow(KompicsEvent,Port,Direction). Matches events by class instead.

answerRequest

public <RQ extends KompicsEvent, RS extends KompicsEvent> TestContext<T> answerRequest(Class<RQ> requestType, Port<? extends PortType> requestPort, Function<RQ, RS> mapper, Port<? extends PortType> responsePort)

Match an outgoing message on the specified requestPort as a request message by calling the provided mapper function with it as an argument. If mapper returns null, then the match fails otherwise the returned message is treated as a response and triggered on responsePort.

Modes - BODY, CONDITIONAL, UNORDERED, ANSWER_REQUEST.

If used in ANSWER_REQUEST mode then the responses are triggered only when all requests have been matched. If in UNORDERED this can be configured. Otherwise responses are triggered immediately.

Parameters:
  • requestType – events of this class are treated as requests.
  • requestPort – port on which to expect request events.
  • mapper – mapper function from a matched request message to a response message otherwise null.
  • responsePort – port on which to trigger a response message.
  • <RQ> – request type.
  • <RS> – response type.
Returns:

current TestContext.

answerRequest

public <RQ extends KompicsEvent, RS extends KompicsEvent> TestContext<T> answerRequest(Class<RQ> requestType, Port<? extends PortType> requestPort, Future<RQ, RS> future)

Match an outgoing message on the specified requestPort as a request message by calling the provided Future.set(KompicsEvent) method of future with an observed event as an argument. If future returns true, then the match succeeded and a response should have been generated such that a subsequent call to Future.get() returns it instead of null. This response can later be used in a call to trigger(Future,Port).

Modes - BODY, CONDITIONAL, UNORDERED, ANSWER_REQUEST.

Parameters:
  • requestType – events of this class are treated as requests.
  • requestPort – port on which to expect request events.
  • future – future to match a request and set a response.
  • <RQ> – request type.
  • <RS> – response type.
Returns:

current TestContext.

answerRequests

public TestContext<T> answerRequests()

Enters ANSWER_REQUEST mode. In this mode, only method answerRequest(Class,Port,Function,Port) can be used. Responses are triggered in their received order, only when all requests have been received.

Allowed modes - BODY, CONDITIONAL.

Returns:current TestContext.

blockExpect

public <P extends PortType> TestContext<T> blockExpect(KompicsEvent event, Port<P> port, Direction direction)

Match the occurrence of the specified event at any position within the sequence of the current block. If the specified event does not occur after executing the last statement of the block, the test case fails on timeout.

Modes - HEADER.

Parameters:
  • event – expected event to match
  • port – port on which event should occur.
  • direction – Direction IN or OUT.
  • <P> – port type.
Returns:

current TestContext

blockExpect

public <P extends PortType, E extends KompicsEvent> TestContext<T> blockExpect(Class<E> eventType, Predicate<E> pred, Port<P> port, Direction direction)

Similar to blockExpect(KompicsEvent,Port,Direction). Matches the expected event with the specified predicate.

blockExpect

public <P extends PortType, E extends KompicsEvent> TestContext<T> blockExpect(Class<E> eventType, Port<P> port, Direction direction)

Similar to blockExpect(KompicsEvent,Port,Direction). Event of the specified class is matched.

body

public TestContext<T> body()

Begin the body of a block.

Modes - HEADER.

Returns:current TestContext.

check

public boolean check()

Run the specified testcase

Returns:true if observed event sequence conforms to testcase otherwise false.

Allowed modes - BODY.

connect

public <P extends PortType> TestContext<T> connect(Negative<P> negative, Positive<P> positive)

Equivalent to connect(negative, positive, Channel.TWO_WAY).

connect

public <P extends PortType> TestContext<T> connect(Positive<P> positive, Negative<P> negative)

Equivalent to connect(positive, negative, Channel.TWO_WAY).

connect

public <P extends PortType> TestContext<T> connect(Negative<P> negative, Positive<P> positive, ChannelFactory factory)

Equivalent to connect(positive, negative, factory).

connect

public <P extends PortType> TestContext<T> connect(Positive<P> positive, Negative<P> negative, ChannelFactory factory)

Connects two ports with the specified ChannelFactory.

Modes - setup HEADER.

Parameters:
Returns:

current TestContext.

create

public <T extends ComponentDefinition> Component create(Class<T> componentDefinition, Init<T> init)

Create a new component as a dependency.

Modes - setup HEADER.

Parameters:
Returns:

created Component

create

public <T extends ComponentDefinition> Component create(Class<T> componentDefinition)

Equivalent to create(componentDefinition, Init.NONE)

create

public <T extends ComponentDefinition> Component create(Class<T> componentDefinition, Init.None init)

Create a new component as a dependency.

Modes - setup HEADER.

Parameters:
Returns:

created Component

disallow

public <P extends PortType> TestContext<T> disallow(KompicsEvent event, Port<P> port, Direction direction)

Blacklist the specified event within the current block. This means that if an event matching the specified one occurs at any position within the block then the test case fails.

Modes - HEADER.

Parameters:
  • event – event to be matched against.
  • port – port on which the matched event should occur.
  • direction – Direction IN or OUT of the event.
  • <P> – port type.
Returns:

current TestContext

disallow

public <E extends KompicsEvent, P extends PortType> TestContext<T> disallow(Class<E> eventType, Predicate<E> predicate, Port<P> port, Direction direction)

Similar to disallow(KompicsEvent,Port,Direction). Matches events only if predicate returns true instead.

disallow

public <E extends KompicsEvent, P extends PortType> TestContext<T> disallow(Class<E> eventType, Port<P> port, Direction direction)

Similar to disallow(KompicsEvent,Port,Direction). Matches event by class instead.

drop

public <P extends PortType> TestContext<T> drop(KompicsEvent event, Port<P> port, Direction direction)

Drops the specified event within the current block. This means that if an event matching the specified one occurs at any position within the block then that message of that event is not forwarded to the recipient(s).

Modes - HEADER.

Parameters:
  • event – event to be matched against.
  • port – port on which the matched event should occur.
  • direction – Direction IN or OUT of the event.
  • <P> – port type.
Returns:

current TestContext

drop

public <E extends KompicsEvent, P extends PortType> TestContext<T> drop(Class<E> eventType, Predicate<E> predicate, Port<P> port, Direction direction)

Similar to drop(KompicsEvent,Port,Direction). Matches events only if predicate returns true instead.

drop

public <E extends KompicsEvent, P extends PortType> TestContext<T> drop(Class<E> eventType, Port<P> port, Direction direction)

Similar to drop(KompicsEvent,Port,Direction). Matches events by class instead.

either

public TestContext<T> either()

Enters a CONDITIONAL mode, and begins creating a new conditional. A conditional consists of either() A or() B end() of which only one of sequences of statements A or B is executed at runtime depending on the observed events. Conditionals can be nested. e.g either() A either() C or() D end() or() B end() is similar to the regular expression A(C|D)|B while either() A or either() C or() D end () B end() is similar to A | (C|D)B.

Modes - BODY, CONDITIONAL.

Returns:current TestContext.

end

public TestContext<T> end()

Mark the end of a a repeat(), either(), unordered(), answerRequests(). Exits the current mode and restores the previous one.

Modes - BODY, UNORDERED, ANSWER_REQUEST, CONDITIONAL.

Returns:current TestContext.

expect

public <P extends PortType> TestContext<T> expect(KompicsEvent event, Port<P> port, Direction direction)

Match the occurrence of a single event going in or out of the component under test. Event equivalence is determined using a comparator provided via method setComparator(Class,Comparator), otherwise it defaults to the Object.equals(Object) method of event.

Modes - BODY, UNORDERED, CONDITIONAL.

Parameters:
  • event – Event message to be matched.
  • port – Port on which event should occur.
  • direction – Direction IN or OUT of expected event.
  • <P> – Port type
Returns:

current TestContext

expect

public <P extends PortType, E extends KompicsEvent> TestContext<T> expect(Class<E> eventType, Predicate<E> predicate, Port<P> port, Direction direction)

Match the occurrence of a single event going in or out of the component under test. The predicate returns true iff a specified event argument is expected.

Modes - BODY, UNORDERED, CONDITIONAL.

Parameters:
  • eventType – Class of expected event.
  • predicate – predicate that determines a matched event message.
  • port – port on which event should occur.
  • direction – Direction IN or OUT of expected event.
  • <P> – port type
  • <E> – Event type
Returns:

current TestContext

expect

public <P extends PortType, E extends KompicsEvent> TestContext<T> expect(Class<E> eventType, Port<P> port, Direction direction)

Match the occurrence of a single event of the specified class, going in or out of the component under test.

Modes - BODY, UNORDERED, CONDITIONAL.

Parameters:
  • eventType – Class of expected event.
  • port – port on which event should occur.
  • direction – Direction IN or OUT of expected event.
  • <P> – port type.
  • <E> – event type.
Returns:

current TestContext.

expectFault

public TestContext<T> expectFault(Class<? extends Throwable> exceptionType)

Verifies that an exception is thrown at the current position in execution. If no exception is thrown the test case fails.

Modes - BODY, CONDITIONAL.

Parameters:
  • exceptionType – class (or superclass) of expected exception
Returns:

current TestContext.

expectFault

public TestContext<T> expectFault(Predicate<Throwable> exceptionPredicate)

Similar to expectFault(Class). Matches thrown exception by specified predicate.

getComponentUnderTest

public Component getComponentUnderTest()

Returns the created component under test.

Modes - All.

Returns:component under test.

inspect

public TestContext<T> inspect(Predicate<T> predicate)

Inspect the internal state of a component. predicate is called by the framework with the ComponentDefinition as an argument. If the predicate returns false, the test case fails immediately.

Modes - BODY, CONDITIONAL.

Parameters:
  • predicate – inspect predicate
Returns:

current TestContext.

newInstance

public static <T extends ComponentDefinition> TestContext<T> newInstance(Class<T> componentDefinition, Init<T> init)

Creates a new TestContext for the specified ComponentDefinition. The init is used to instantiate componentDefinition.

Parameters:
  • componentDefinitionComponentDefinition class
  • initInit to instantiate componentDefinition
  • <T>componentDefinition type
Returns:

a new TestContext for the specified ComponentDefinition.

newInstance

public static <T extends ComponentDefinition> TestContext<T> newInstance(Class<T> componentDefinition)

Equivalent to newInstance(componentDefinition, Init.NONE).

newInstance

public static <T extends ComponentDefinition> TestContext<T> newInstance(Class<T> componentDefinition, Init.None init)

Creates a new TestContext for the specified ComponentDefinition. The init is used to instantiate componentDefinition.

Parameters:
Returns:

a new TestContext for the specified ComponentDefinition.

or

public TestContext<T> or()

Begins the ‘or’ sequence of a conditional statment. Must be called exactly once for the current conditional statement.

Modes - CONDITIONAL.

Returns:current TestContext.

repeat

public TestContext<T> repeat(int times)

Enters HEADER mode and begins creating a block. The block matches a sequence the specified number of times.

Modes - BODY, CONDITIONAL.

Parameters:
  • times – number of times to match a sequence.
Returns:

current TestContext.

repeat

public TestContext<T> repeat()

Enters HEADER mode and begins creating a block. The block matches a sequence zero or more times.

Modes - BODY, CONDITIONAL.

Returns:current TestContext.

repeat

public TestContext<T> repeat(int times, EntryFunction entryFunction)

Enters HEADER mode and begins creating a block. The block matches a sequence the specified number of times. The entryFunction is executed at the beginning of every iteration of the block.

Modes - BODY, CONDITIONAL.

Parameters:
  • times – number of times to match a sequence.
  • entryFunctionEntryFunction implementation to be called at the beginning of each iteration of the created block.
Returns:

current TestContext.

repeat

public TestContext<T> repeat(EntryFunction entryFunction)

Enters HEADER mode and begins creating a block. The block matches a sequence zero or more times. The entryFunction is executed at the beginning of every iteration of the block -

Allowed modes - BODY, CONDITIONAL.

Parameters:
Returns:

current TestContext.

setComparator

public <E extends KompicsEvent> TestContext<T> setComparator(Class<E> eventType, Comparator<E> comparator)

Set a comparator for comparing events of the specified class. The comparator is used for determining the equivalence of two messages of the same class. If no comparator is provided for that class, then the Object.equals(Object) of that class is used.

Modes - setup HEADER.

Parameters:
  • eventType – class of event.
  • comparator – comparator for eventType.
  • <E> – eventType.
Returns:

current TestContext

setDefaultAction

public <E extends KompicsEvent> TestContext<T> setDefaultAction(Class<E> eventType, Function<E, Action> function)

Set policy for handling unmatched/unexpected/unspecified events. If such an event of class eventType is observed, then function is called with it as an argument. If the return value of function is null then the test case fails. Otherwise, an Action is returned determining whether to drop, whitelist or blacklist M.

Modes - setup HEADER.

Parameters:
  • eventType – classes (and subclasses) of events to handle with function.
  • function – function to specify the taken action for an event.
  • <E> – eventType.
Returns:

current TestContext.

setTimeout

public TestContext<T> setTimeout(long timeoutMS)

Specify timeout value to wait for an event to occur.

Modes - first (initial) HEADER.

Parameters:
  • timeoutMS – timeout in milliseconds
Returns:

current TestContext.

trigger

public <P extends PortType> TestContext<T> trigger(KompicsEvent event, Port<P> port)

Trigger an event on the specified port.

Modes - BODY, CONDITIONAL.

Parameters:
  • event – event to be triggered.
  • port – port on which to trigger event.
  • <P> – port type.
Returns:

current TestContext.

trigger

public <RQ extends KompicsEvent, RS extends KompicsEvent, P extends PortType> TestContext<T> trigger(Future<RQ, RS> future, Port<P> responsePort)

Trigger the event provided by future as a response on specified port. The future must have been setin a previous call to answerRequest(Class,Port,Future). The Future.get() method is called to retrieve the triggered event.

Modes - BODY, CONDITIONAL.

Parameters:
  • responsePort – port on which event is triggered.
  • future – future providing event to be triggered.
  • <RQ> – request event type.
  • <RS> – response event type.
  • <P> – port type.
Returns:

current TestContext.

unordered

public TestContext<T> unordered()

Equivalent to unordered(true).

Returns:current TestContext.

unordered

public TestContext<T> unordered(boolean immediateResponse)

Enters MODE.UNORDERED. Allowed method calls in this mode are:

expect(KompicsEvent,Port,Direction)expect(Class,Port,Direction)expect(Class,Predicate,Port,Direction)answerRequest(Class,Port,Future)answerRequest(Class,Port,Function,Port)

Match events in the order that they occur at runtime as opposed to their sequential specified order. When answering requests in this mode, setting immediateResponse to true causes each response event to be triggered as soon the request is matched, otherwise it is triggered when all requests have been matched. The immediateResponse flag has no effect when using answerRequest(Class,Port,Future).

Modes - BODY, CONDITIONAL.

Parameters:
  • immediateResponse
    • where applicable, respond to requests immediately(true) or when all events have been received(false).
Returns:

current TestContext.