Return-Path: X-Original-To: archive-asf-public-internal@cust-asf2.ponee.io Delivered-To: archive-asf-public-internal@cust-asf2.ponee.io Received: from cust-asf.ponee.io (cust-asf.ponee.io [163.172.22.183]) by cust-asf2.ponee.io (Postfix) with ESMTP id ECDDB200B32 for ; Thu, 23 Jun 2016 23:18:35 +0200 (CEST) Received: by cust-asf.ponee.io (Postfix) id EB853160A59; Thu, 23 Jun 2016 21:18:35 +0000 (UTC) Delivered-To: archive-asf-public@cust-asf.ponee.io Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by cust-asf.ponee.io (Postfix) with SMTP id 19279160A35 for ; Thu, 23 Jun 2016 23:18:34 +0200 (CEST) Received: (qmail 83009 invoked by uid 500); 23 Jun 2016 21:18:34 -0000 Mailing-List: contact users-help@apex.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: users@apex.apache.org Delivered-To: mailing list users@apex.apache.org Received: (qmail 82999 invoked by uid 99); 23 Jun 2016 21:18:33 -0000 Received: from pnap-us-west-generic-nat.apache.org (HELO spamd2-us-west.apache.org) (209.188.14.142) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 23 Jun 2016 21:18:33 +0000 Received: from localhost (localhost [127.0.0.1]) by spamd2-us-west.apache.org (ASF Mail Server at spamd2-us-west.apache.org) with ESMTP id F357A1A5759 for ; Thu, 23 Jun 2016 21:18:32 +0000 (UTC) X-Virus-Scanned: Debian amavisd-new at spamd2-us-west.apache.org X-Spam-Flag: NO X-Spam-Score: 1.179 X-Spam-Level: * X-Spam-Status: No, score=1.179 tagged_above=-999 required=6.31 tests=[DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, HTML_MESSAGE=2, RCVD_IN_DNSWL_LOW=-0.7, RCVD_IN_MSPIKE_H3=-0.01, RCVD_IN_MSPIKE_WL=-0.01, SPF_PASS=-0.001] autolearn=disabled Authentication-Results: spamd2-us-west.apache.org (amavisd-new); dkim=pass (2048-bit key) header.d=gmail.com Received: from mx2-lw-eu.apache.org ([10.40.0.8]) by localhost (spamd2-us-west.apache.org [10.40.0.9]) (amavisd-new, port 10024) with ESMTP id DGGAeRApBRKu for ; Thu, 23 Jun 2016 21:18:28 +0000 (UTC) Received: from mail-qk0-f177.google.com (mail-qk0-f177.google.com [209.85.220.177]) by mx2-lw-eu.apache.org (ASF Mail Server at mx2-lw-eu.apache.org) with ESMTPS id 458825F260 for ; Thu, 23 Jun 2016 21:18:28 +0000 (UTC) Received: by mail-qk0-f177.google.com with SMTP id c73so123574580qkg.2 for ; Thu, 23 Jun 2016 14:18:28 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:from:date:message-id:subject:to; bh=jAg2jN8sDd7QJW3Iht+BN5uoIc2beu/bsG+KbhtMMvI=; b=b/0Q1J8gHJAltVfcPU04VntL4cqBw92TNF8gjQLJ7Mloj8/RvjN3P4Hes/if6/EnKy 6eVPlatk+ATmWfJNu9DMj6dx1Gll/JQzU8pBrBWDY5tQbvGY6fXxT6gE0LiUD36DGVtQ KpDDljkLIfUL5qElW+P2DfwUelq3vk3Du8SjXCLGCVjRZovWmoG4t7CGDBTVMX3JPcwx 2c8jafklF247TwKCrpRod6QopT8jL8LfVgCgOLGYtQmgrkFwqbWvWmhufBa+5YnlYBc9 NZbaZSz/HZeyLFGbNmilfZFzCkzrY/MNZg5Tgk7bWpEaoJkKMODmfQhfyjDClT3gxQr4 lMaQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:from:date:message-id:subject:to; bh=jAg2jN8sDd7QJW3Iht+BN5uoIc2beu/bsG+KbhtMMvI=; b=EG1jEpyBF+ZkjeuIiWGuFzFXiu2brUm8ic9IPuUXO5Ifc9FFsjBm+lfliXSV1Tb1yY FMkZpdFZqpK9F1N+QCD2fi3L1uwJu4ctbY7FmGBtYKv0or5EmgXAyniH4FQGNtwJ73lG ZAZjInbrcygjVyuimNOpc1+FYW2kVnbNRIj3BYLQmPcHQ5EOkI5AfpIe1jvoBH92EAj1 btrUtPHwlcbWaIulN6Ak2UCa+sMebdVO58W4G7gkRXTnAYRQ0lcMRuyGSLDkHNi2GxrI 4rE1cAgBWWR6tH7/+3tTYo+EmfDtGg9GE+RfwDY/ob1qSpeFxEDzFYJELlHjJySfsRq/ IDFw== X-Gm-Message-State: ALyK8tIU8yz1Dr2htCGoSnpLjFFH1d9JbFo92X5RSjUp6Yc7MSwsWkjaegWP2Rsp6bubi8HFW2GzLllTym9mNg== X-Received: by 10.200.53.142 with SMTP id k14mr754690qtb.63.1466716707361; Thu, 23 Jun 2016 14:18:27 -0700 (PDT) MIME-Version: 1.0 Received: by 10.55.143.1 with HTTP; Thu, 23 Jun 2016 14:18:26 -0700 (PDT) From: Thomas Weise Date: Thu, 23 Jun 2016 14:18:26 -0700 Message-ID: Subject: How to write a unit test that runs embedded cluster and verifies results To: users@apex.apache.org Content-Type: multipart/alternative; boundary=001a11425e3e5436050535f898a6 archived-at: Thu, 23 Jun 2016 21:18:36 -0000 --001a11425e3e5436050535f898a6 Content-Type: text/plain; charset=UTF-8 Posting it here as the question comes up often. When testing multiple operators in a DAG, how do I know that it "worked"? Following is the anti-pattern: @Test public void testSomeMethod() throws Exception { LocalMode lma = LocalMode.newInstance(); lma.prepareDAG(new Application(), new Configuration(false)); LocalMode.Controller lc = lma.getController(); * lc.run();* } Something could have gone wrong or maybe the test never terminates and blocks the entire test suite. Instead, I can write it to run the DAG (asynchronously) *and* verify the expected results by polling with a timeout. Examples: https://github.com/DataTorrent/examples/blob/master/tutorials/exactly-once/src/test/java/com/example/myapexapp/ApplicationTest.java Writes to DB, uses DB to poll for results. https://github.com/DataTorrent/examples/blob/master/tutorials/unifiers/src/test/java/com/example/myapexapp/ApplicationTest.java Collects data in memory and terminates when expected result is found. You can also use BaseOperator.shutdown within the operator thread and the operator and all its downstream dependencies will terminate. When that is done from the (only) input operator, it will terminate the entire DAG and cause control to return to the unit test thread (when using synchronous run). Thomas --001a11425e3e5436050535f898a6 Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable
Posting it here as the question comes up often. When = testing multiple operators in a DAG, how do I know that it "worked&quo= t;? Following is the anti-pattern:

=C2=A0 @Te= st
=C2=A0 public void testSomeMethod() throws Exception
=C2=A0 {
=C2=A0 =C2=A0 LocalMode lma =3D LocalMode.newInstance()= ;
=C2=A0 =C2=A0 lma.prepareDAG(new Application(),=C2=A0new Config= uration(false));
=C2=A0 =C2=A0 LocalMode.Controller lc =3D lm= a.getController();
=C2=A0 =C2=A0 lc.run(<millis>);
=C2=A0 }

Something could ha= ve gone wrong or maybe the test never terminates and blocks the entire test= suite.=C2=A0

Instead, I can write it to run t= he DAG (asynchronously)=C2=A0and verify the expected results by poll= ing with a timeout. Examples:


Writes to D= B, uses DB to poll for results.


Collects data in = memory and terminates when expected result is found.

You can also use BaseOperator.shutdown within the operator thread and th= e operator and all its downstream dependencies will terminate. When that is= done from the (only) input operator, it will terminate the entire DAG and = cause control to return to the unit test thread (when using synchronous run= ).

Thomas


--001a11425e3e5436050535f898a6--