SlideShare a Scribd company logo
Asynchronen 
Code testen 
@ndrssmn 
Andreas Simon
Synchron 
Übertragungszeit 
Wartezeit 
Bearbeitungszeit 
Wartezeit 
Antwortzeit
Asynchron 
Antwortzeit 
Wartezeit
Fehlertoleranz 
Verfügbarkeit 
Parallelisierung 
Performance 
Event-Driven Architecture
Listening 
Callback
Synchronisation 
TIMEOUT
Listening in JUnit 
@Test public void 
should_reply_with_Fibonacci_numbers() throws Exception { 
// Arrange 
NotificationTrace<Integer> trace = new NotificationTrace<>(TIMEOUT); 
String replyQueue = channel.queueDeclare().getQueue(); 
FibonacciCalculator.create(connection.createChannel()); 
new IntegerConsumer( 
connection.createChannel(), 
trace::append) 
.consumeQueue(replyQueue); 
// Act 
publishNumbers(MIN, MAX, replyQueue); 
// Assert 
trace.containsNotification(equalTo(FIB_MIN)); 
trace.containsNotification(equalTo(FIB_MAX)); 
}
Listening in Mocha 
describe('AMQP Fibonacci service', function() { 
it('calculates fib(' + MIN + ')', function(done) { 
connection.on('ready', function () { 
connection.queue('my-queue', function(q) { 
q.subscribe(function (message) { 
try { 
// Assert 
message.data.toString().should.eql(FIB_MIN); 
done(); 
} catch(e) { 
done(e); 
} 
}); 
// Act 
connection.publish( 
'calculate-fibonacci', MIN, 
{ replyTo: 'my-queue'} 
); 
}); 
}); 
});
Sampling 
POST localhost/ 
CREATED Location: localhost/30 
GET localhost/30 
NOT FOUND 
GET localhost/30 
NOT FOUND 
GET localhost/30 
OK :: 832040 
TIMEOUT
Sampling in JUnit 
@Test public void 
calculates_fib_30() throws Exception { 
// Act 
connection = POST("http://localhost:3000/", "30"); 
fibLocation = connection.getHeaderField("Location"); 
// Assert 
Probe probe = responseTo( 
fibLocation, 
equalTo(Integer.toString(FIB_30)) 
); 
new Poller(TIMEOUT, POLL_DELAY).check(probe); 
}
public class Poller { 
[…] 
public void check(Probe probe) { 
[…] 
while (!probe.isSatisfied()) { 
[…] 
Thread.sleep(pollDelayMillis); 
probe.sample(); 
} 
} 
} 
public interface Probe { 
void sample(); 
boolean isSatisfied(); 
void describeAcceptanceCriteriaTo(Description d); 
void describeFailureTo(Description d); 
}
Sampling in Mocha 
describe('Fibonacci server', function() { 
it('should calculate fib(20)', function(done) { 
var req = http.request(POST_fib, function(res) { 
res.setEncoding('utf8'); 
res.statusCode.should.eql(201); 
res.headers.location.should.be.ok; 
pollGET(res.headers.location, done); 
}).on('error', done); 
req.setHeader('Content-Type', 'application/x-www-form- 
urlencoded;charset=UTF-8'); 
req.write('n=20n'); 
req.end(); 
});
Sampling in Mocha 
function pollGET(url, done) { 
http.get(url, function(res) { 
if(200 != res.statusCode) { 
setTimeout(pollGET, POLL_DELAY, url, done); 
} 
res.on('data', function (chunk) { 
chunk.toString().should.eql('6765'); 
done(); 
}); 
}).on('error', done); 
}
Test the test 
@Test public void 
is_thread_safe() throws Exception { 
latch = startStressing(STRESSING_THREADS, () -> { 
for (int i = 0; i < ITERATIONS; i++) { 
trace.append("NOT-WANTED"); 
Thread.sleep(SLEEPTIME); 
} 
latch.countDown(); 
}); 
scheduler.schedule( 
() -> trace.append("WANTED"), 
100, TimeUnit.MILLISECONDS 
); 
trace.containsNotification(equalTo("WANTED")); 
latch.await(); 
assertThat( 
trace.getAppendCount(), 
is(equalTo((long) STRESSING_THREADS * ITERATIONS + 1)) 
); 
}
Thread-sicher implementieren 
public class NotificationTrace<T> { 
public void append(T message) { 
synchronized (traceLock) { 
trace.add(message); 
traceLock.notifyAll(); 
} 
} 
public void containsNotification(Matcher<? super T> criteria) 
throws AssertionError, InterruptedException { 
Timeout timeout = new Timeout(timeoutMs); 
synchronized (traceLock) { 
stream = new NotificationStream<>(trace, criteria); 
while (!stream.hasMatched()) { 
if (timeout.hasTimedOut()) { 
throw new AssertionError(); 
} 
timeout.waitOn(traceLock); 
} 
} 
}
Aufräumen 
@After 
public void tearDown() throws InterruptedException { 
executorService.shutdownNow(); 
scheduler.shutdownNow(); 
}
Fazit 
Listening vs. Sampling 
Synchronisierungsmechanismen kapseln (und durch Unit-Tests 
validieren) 
Brian Goetz: "Java Concurrency in Practice" 
Nat Pryce, Steve Freeman: "Growing Object-Oriented Software" 
https://github.com/andreassimon/talk-asynchronen-code-testen
Quagilis 
Andreas Simon 
Lazarettstr. 9 
48147 Münster 
Fon +49 (0) 251 - 590 491 55-0 
Fax +49 (0) 251 - 590 491 55-9 
a.simon@quagilis.de 
http://www.quagilis.de 
Quality in Agile.

More Related Content

What's hot

OWASP Proxy
OWASP ProxyOWASP Proxy
OWASP Proxy
Security B-Sides
 
for this particular program how do i create the input innotepad 1st ? #includ...
for this particular program how do i create the input innotepad 1st ? #includ...for this particular program how do i create the input innotepad 1st ? #includ...
for this particular program how do i create the input innotepad 1st ? #includ...
hwbloom59
 
Gevent rabbit rpc
Gevent rabbit rpcGevent rabbit rpc
Gevent rabbit rpc
Aleksandr Mokrov
 
Relayd: a load balancer for OpenBSD
Relayd: a load balancer for OpenBSD Relayd: a load balancer for OpenBSD
Relayd: a load balancer for OpenBSD
Giovanni Bechis
 
Introducing to Asynchronous Programming
Introducing to Asynchronous  ProgrammingIntroducing to Asynchronous  Programming
Introducing to Asynchronous Programming
Александр Федоров
 
for this particular program how do i create the input innotepad 1st ?#include...
for this particular program how do i create the input innotepad 1st ?#include...for this particular program how do i create the input innotepad 1st ?#include...
for this particular program how do i create the input innotepad 1st ?#include...
hwbloom14
 
Devinsampa nginx-scripting
Devinsampa nginx-scriptingDevinsampa nginx-scripting
Devinsampa nginx-scriptingTony Fabeen
 
Kansai.pm 10周年記念 Plack/PSGI 入門
Kansai.pm 10周年記念 Plack/PSGI 入門Kansai.pm 10周年記念 Plack/PSGI 入門
Kansai.pm 10周年記念 Plack/PSGI 入門lestrrat
 
The Ring programming language version 1.10 book - Part 35 of 212
The Ring programming language version 1.10 book - Part 35 of 212The Ring programming language version 1.10 book - Part 35 of 212
The Ring programming language version 1.10 book - Part 35 of 212
Mahmoud Samir Fayed
 
Shell Script Disk Usage Report and E-Mail Current Threshold Status
Shell Script  Disk Usage Report and E-Mail Current Threshold StatusShell Script  Disk Usage Report and E-Mail Current Threshold Status
Shell Script Disk Usage Report and E-Mail Current Threshold Status
VCP Muthukrishna
 
Anatomy of a PHP Request ( UTOSC 2010 )
Anatomy of a PHP Request ( UTOSC 2010 )Anatomy of a PHP Request ( UTOSC 2010 )
Anatomy of a PHP Request ( UTOSC 2010 )Joseph Scott
 
The Ring programming language version 1.2 book - Part 16 of 84
The Ring programming language version 1.2 book - Part 16 of 84The Ring programming language version 1.2 book - Part 16 of 84
The Ring programming language version 1.2 book - Part 16 of 84
Mahmoud Samir Fayed
 
Perl Sucks - and what to do about it
Perl Sucks - and what to do about itPerl Sucks - and what to do about it
Perl Sucks - and what to do about it
2shortplanks
 
Cooking pies with Celery
Cooking pies with CeleryCooking pies with Celery
Cooking pies with Celery
Aleksandr Mokrov
 
ZeroMQ Is The Answer
ZeroMQ Is The AnswerZeroMQ Is The Answer
ZeroMQ Is The Answer
Ian Barber
 
Perl web app 테스트전략
Perl web app 테스트전략Perl web app 테스트전략
Perl web app 테스트전략
Jeen Lee
 
とにかく始めるClojure
とにかく始めるClojureとにかく始めるClojure
とにかく始めるClojure
Masayuki Muto
 
Python event based network sniffer
Python event based network snifferPython event based network sniffer
Python event based network sniffer
Jirka Vejrazka
 
ZeroMQ: Messaging Made Simple
ZeroMQ: Messaging Made SimpleZeroMQ: Messaging Made Simple
ZeroMQ: Messaging Made Simple
Ian Barber
 

What's hot (20)

OWASP Proxy
OWASP ProxyOWASP Proxy
OWASP Proxy
 
for this particular program how do i create the input innotepad 1st ? #includ...
for this particular program how do i create the input innotepad 1st ? #includ...for this particular program how do i create the input innotepad 1st ? #includ...
for this particular program how do i create the input innotepad 1st ? #includ...
 
Gevent rabbit rpc
Gevent rabbit rpcGevent rabbit rpc
Gevent rabbit rpc
 
Relayd: a load balancer for OpenBSD
Relayd: a load balancer for OpenBSD Relayd: a load balancer for OpenBSD
Relayd: a load balancer for OpenBSD
 
Introducing to Asynchronous Programming
Introducing to Asynchronous  ProgrammingIntroducing to Asynchronous  Programming
Introducing to Asynchronous Programming
 
for this particular program how do i create the input innotepad 1st ?#include...
for this particular program how do i create the input innotepad 1st ?#include...for this particular program how do i create the input innotepad 1st ?#include...
for this particular program how do i create the input innotepad 1st ?#include...
 
Devinsampa nginx-scripting
Devinsampa nginx-scriptingDevinsampa nginx-scripting
Devinsampa nginx-scripting
 
Kansai.pm 10周年記念 Plack/PSGI 入門
Kansai.pm 10周年記念 Plack/PSGI 入門Kansai.pm 10周年記念 Plack/PSGI 入門
Kansai.pm 10周年記念 Plack/PSGI 入門
 
The Ring programming language version 1.10 book - Part 35 of 212
The Ring programming language version 1.10 book - Part 35 of 212The Ring programming language version 1.10 book - Part 35 of 212
The Ring programming language version 1.10 book - Part 35 of 212
 
Send mail-oracle11g-det
Send mail-oracle11g-detSend mail-oracle11g-det
Send mail-oracle11g-det
 
Shell Script Disk Usage Report and E-Mail Current Threshold Status
Shell Script  Disk Usage Report and E-Mail Current Threshold StatusShell Script  Disk Usage Report and E-Mail Current Threshold Status
Shell Script Disk Usage Report and E-Mail Current Threshold Status
 
Anatomy of a PHP Request ( UTOSC 2010 )
Anatomy of a PHP Request ( UTOSC 2010 )Anatomy of a PHP Request ( UTOSC 2010 )
Anatomy of a PHP Request ( UTOSC 2010 )
 
The Ring programming language version 1.2 book - Part 16 of 84
The Ring programming language version 1.2 book - Part 16 of 84The Ring programming language version 1.2 book - Part 16 of 84
The Ring programming language version 1.2 book - Part 16 of 84
 
Perl Sucks - and what to do about it
Perl Sucks - and what to do about itPerl Sucks - and what to do about it
Perl Sucks - and what to do about it
 
Cooking pies with Celery
Cooking pies with CeleryCooking pies with Celery
Cooking pies with Celery
 
ZeroMQ Is The Answer
ZeroMQ Is The AnswerZeroMQ Is The Answer
ZeroMQ Is The Answer
 
Perl web app 테스트전략
Perl web app 테스트전략Perl web app 테스트전략
Perl web app 테스트전략
 
とにかく始めるClojure
とにかく始めるClojureとにかく始めるClojure
とにかく始めるClojure
 
Python event based network sniffer
Python event based network snifferPython event based network sniffer
Python event based network sniffer
 
ZeroMQ: Messaging Made Simple
ZeroMQ: Messaging Made SimpleZeroMQ: Messaging Made Simple
ZeroMQ: Messaging Made Simple
 

Viewers also liked

Event Driven - Node.JS
Event Driven - Node.JSEvent Driven - Node.JS
Event Driven - Node.JSMike Bild
 
Classifications in IBM Maximo Asset Management
Classifications in IBM Maximo Asset ManagementClassifications in IBM Maximo Asset Management
Classifications in IBM Maximo Asset Management
Robert Zientara
 
IBM Mobility Strategie - Zukunft der Informationstechnologie
IBM Mobility Strategie - Zukunft der InformationstechnologieIBM Mobility Strategie - Zukunft der Informationstechnologie
IBM Mobility Strategie - Zukunft der Informationstechnologie
IBM Lotus
 
Meters in IBM Maximo Asset Management
Meters in IBM Maximo Asset ManagementMeters in IBM Maximo Asset Management
Meters in IBM Maximo Asset Management
Robert Zientara
 
Buscadores y metabuscadores
Buscadores y metabuscadores Buscadores y metabuscadores
Buscadores y metabuscadores Ddbere
 
Catalogo Calzados Narrow Invierno 2015
Catalogo Calzados Narrow Invierno 2015Catalogo Calzados Narrow Invierno 2015
Catalogo Calzados Narrow Invierno 2015
Elas Del Jean
 
Forderung arbeitnehmer 2012
Forderung arbeitnehmer 2012Forderung arbeitnehmer 2012
Forderung arbeitnehmer 2012brassist
 
Importancia de la computación
Importancia de la computaciónImportancia de la computación
Importancia de la computación
darbry
 
Presentación
PresentaciónPresentación
Presentación
Liliana Sanchez
 
Socialmediarecruiting2015 120626023458 Phpapp02
Socialmediarecruiting2015 120626023458 Phpapp02Socialmediarecruiting2015 120626023458 Phpapp02
Socialmediarecruiting2015 120626023458 Phpapp02dhoncu
 
Nancy nucete
Nancy nucete Nancy nucete
Nancy nucete
Nucete
 
El internet
El internetEl internet
El internet
carlavanneromero
 
La Web 2.0
La Web 2.0 La Web 2.0
La Web 2.0
Rocio Rojas
 
Rubrica competencia siglo 21
Rubrica competencia siglo 21Rubrica competencia siglo 21
Rubrica competencia siglo 21
mariaeugeniallanos
 
Singaña vanessa analizar software educativo
Singaña vanessa analizar software educativoSingaña vanessa analizar software educativo
Singaña vanessa analizar software educativo
vane01si
 
Grandes entrenadores baloncesto
Grandes entrenadores baloncestoGrandes entrenadores baloncesto
Grandes entrenadores baloncesto
hectorini1
 
Proyecto tic digaroba
Proyecto tic digarobaProyecto tic digaroba
Proyecto tic digaroba
Daniel Vergara
 
LA WEB 2.0 Y NUESTRA VISIÓN INTEGRAL
LA WEB 2.0 Y NUESTRA VISIÓN INTEGRALLA WEB 2.0 Y NUESTRA VISIÓN INTEGRAL
LA WEB 2.0 Y NUESTRA VISIÓN INTEGRAL
Nucete
 

Viewers also liked (20)

Event Driven - Node.JS
Event Driven - Node.JSEvent Driven - Node.JS
Event Driven - Node.JS
 
Classifications in IBM Maximo Asset Management
Classifications in IBM Maximo Asset ManagementClassifications in IBM Maximo Asset Management
Classifications in IBM Maximo Asset Management
 
IBM Mobility Strategie - Zukunft der Informationstechnologie
IBM Mobility Strategie - Zukunft der InformationstechnologieIBM Mobility Strategie - Zukunft der Informationstechnologie
IBM Mobility Strategie - Zukunft der Informationstechnologie
 
Meters in IBM Maximo Asset Management
Meters in IBM Maximo Asset ManagementMeters in IBM Maximo Asset Management
Meters in IBM Maximo Asset Management
 
Buscadores y metabuscadores
Buscadores y metabuscadores Buscadores y metabuscadores
Buscadores y metabuscadores
 
Catalogo Calzados Narrow Invierno 2015
Catalogo Calzados Narrow Invierno 2015Catalogo Calzados Narrow Invierno 2015
Catalogo Calzados Narrow Invierno 2015
 
Das museum
Das museumDas museum
Das museum
 
Forderung arbeitnehmer 2012
Forderung arbeitnehmer 2012Forderung arbeitnehmer 2012
Forderung arbeitnehmer 2012
 
Importancia de la computación
Importancia de la computaciónImportancia de la computación
Importancia de la computación
 
Presentación
PresentaciónPresentación
Presentación
 
Socialmediarecruiting2015 120626023458 Phpapp02
Socialmediarecruiting2015 120626023458 Phpapp02Socialmediarecruiting2015 120626023458 Phpapp02
Socialmediarecruiting2015 120626023458 Phpapp02
 
Nancy nucete
Nancy nucete Nancy nucete
Nancy nucete
 
El internet
El internetEl internet
El internet
 
La Web 2.0
La Web 2.0 La Web 2.0
La Web 2.0
 
Rubrica competencia siglo 21
Rubrica competencia siglo 21Rubrica competencia siglo 21
Rubrica competencia siglo 21
 
CV ALDO SAAVEDRA
CV ALDO SAAVEDRACV ALDO SAAVEDRA
CV ALDO SAAVEDRA
 
Singaña vanessa analizar software educativo
Singaña vanessa analizar software educativoSingaña vanessa analizar software educativo
Singaña vanessa analizar software educativo
 
Grandes entrenadores baloncesto
Grandes entrenadores baloncestoGrandes entrenadores baloncesto
Grandes entrenadores baloncesto
 
Proyecto tic digaroba
Proyecto tic digarobaProyecto tic digaroba
Proyecto tic digaroba
 
LA WEB 2.0 Y NUESTRA VISIÓN INTEGRAL
LA WEB 2.0 Y NUESTRA VISIÓN INTEGRALLA WEB 2.0 Y NUESTRA VISIÓN INTEGRAL
LA WEB 2.0 Y NUESTRA VISIÓN INTEGRAL
 

Similar to Asynchronen Code testen

Callbacks, Promises, and Coroutines (oh my!): Asynchronous Programming Patter...
Callbacks, Promises, and Coroutines (oh my!): Asynchronous Programming Patter...Callbacks, Promises, and Coroutines (oh my!): Asynchronous Programming Patter...
Callbacks, Promises, and Coroutines (oh my!): Asynchronous Programming Patter...
Domenic Denicola
 
Unit testing CourseSites Apache Filter
Unit testing CourseSites Apache FilterUnit testing CourseSites Apache Filter
Unit testing CourseSites Apache Filter
Wayan Wira
 
Workshop 5: JavaScript testing
Workshop 5: JavaScript testingWorkshop 5: JavaScript testing
Workshop 5: JavaScript testing
Visual Engineering
 
Akka Cluster in Java - JCConf 2015
Akka Cluster in Java - JCConf 2015Akka Cluster in Java - JCConf 2015
Akka Cluster in Java - JCConf 2015
Jiayun Zhou
 
Bonnes pratiques de développement avec Node js
Bonnes pratiques de développement avec Node jsBonnes pratiques de développement avec Node js
Bonnes pratiques de développement avec Node js
Francois Zaninotto
 
Creating a Facebook Clone - Part XXVII - Transcript.pdf
Creating a Facebook Clone - Part XXVII - Transcript.pdfCreating a Facebook Clone - Part XXVII - Transcript.pdf
Creating a Facebook Clone - Part XXVII - Transcript.pdf
ShaiAlmog1
 
Embracing the-power-of-refactor
Embracing the-power-of-refactorEmbracing the-power-of-refactor
Embracing the-power-of-refactor
Xiaojun REN
 
COScheduler In Depth
COScheduler In DepthCOScheduler In Depth
COScheduler In Depth
WO Community
 
Jersey Guice AOP
Jersey Guice AOPJersey Guice AOP
Jersey Guice AOP
Domenico Briganti
 
Ajax - a quick introduction
Ajax - a quick introductionAjax - a quick introduction
Ajax - a quick introduction
Stefan Pettersson
 
Anton Moldovan "Load testing which you always wanted"
Anton Moldovan "Load testing which you always wanted"Anton Moldovan "Load testing which you always wanted"
Anton Moldovan "Load testing which you always wanted"
Fwdays
 
33rd Degree 2013, Bad Tests, Good Tests
33rd Degree 2013, Bad Tests, Good Tests33rd Degree 2013, Bad Tests, Good Tests
33rd Degree 2013, Bad Tests, Good TestsTomek Kaczanowski
 
Unit Testing Express and Koa Middleware in ES2015
Unit Testing Express and Koa Middleware in ES2015Unit Testing Express and Koa Middleware in ES2015
Unit Testing Express and Koa Middleware in ES2015
Morris Singer
 
Asynchronous programming done right - Node.js
Asynchronous programming done right - Node.jsAsynchronous programming done right - Node.js
Asynchronous programming done right - Node.js
Piotr Pelczar
 
Server Side Swift: Vapor
Server Side Swift: VaporServer Side Swift: Vapor
Server Side Swift: Vapor
Paweł Kowalczuk
 
Reactive programming on Android
Reactive programming on AndroidReactive programming on Android
Reactive programming on Android
Tomáš Kypta
 
Going fullstack React(ive) - Paulo Lopes - Codemotion Amsterdam 2017
Going fullstack React(ive) - Paulo Lopes - Codemotion Amsterdam 2017Going fullstack React(ive) - Paulo Lopes - Codemotion Amsterdam 2017
Going fullstack React(ive) - Paulo Lopes - Codemotion Amsterdam 2017
Codemotion
 
Distributed load testing with k6
Distributed load testing with k6Distributed load testing with k6
Distributed load testing with k6
Thijs Feryn
 
Unit Testing Express Middleware
Unit Testing Express MiddlewareUnit Testing Express Middleware
Unit Testing Express Middleware
Morris Singer
 

Similar to Asynchronen Code testen (20)

Callbacks, Promises, and Coroutines (oh my!): Asynchronous Programming Patter...
Callbacks, Promises, and Coroutines (oh my!): Asynchronous Programming Patter...Callbacks, Promises, and Coroutines (oh my!): Asynchronous Programming Patter...
Callbacks, Promises, and Coroutines (oh my!): Asynchronous Programming Patter...
 
Unit testing CourseSites Apache Filter
Unit testing CourseSites Apache FilterUnit testing CourseSites Apache Filter
Unit testing CourseSites Apache Filter
 
Workshop 5: JavaScript testing
Workshop 5: JavaScript testingWorkshop 5: JavaScript testing
Workshop 5: JavaScript testing
 
Akka Cluster in Java - JCConf 2015
Akka Cluster in Java - JCConf 2015Akka Cluster in Java - JCConf 2015
Akka Cluster in Java - JCConf 2015
 
Bonnes pratiques de développement avec Node js
Bonnes pratiques de développement avec Node jsBonnes pratiques de développement avec Node js
Bonnes pratiques de développement avec Node js
 
Creating a Facebook Clone - Part XXVII - Transcript.pdf
Creating a Facebook Clone - Part XXVII - Transcript.pdfCreating a Facebook Clone - Part XXVII - Transcript.pdf
Creating a Facebook Clone - Part XXVII - Transcript.pdf
 
Embracing the-power-of-refactor
Embracing the-power-of-refactorEmbracing the-power-of-refactor
Embracing the-power-of-refactor
 
COScheduler In Depth
COScheduler In DepthCOScheduler In Depth
COScheduler In Depth
 
Jersey Guice AOP
Jersey Guice AOPJersey Guice AOP
Jersey Guice AOP
 
Ajax - a quick introduction
Ajax - a quick introductionAjax - a quick introduction
Ajax - a quick introduction
 
Anton Moldovan "Load testing which you always wanted"
Anton Moldovan "Load testing which you always wanted"Anton Moldovan "Load testing which you always wanted"
Anton Moldovan "Load testing which you always wanted"
 
33rd Degree 2013, Bad Tests, Good Tests
33rd Degree 2013, Bad Tests, Good Tests33rd Degree 2013, Bad Tests, Good Tests
33rd Degree 2013, Bad Tests, Good Tests
 
Unit Testing Express and Koa Middleware in ES2015
Unit Testing Express and Koa Middleware in ES2015Unit Testing Express and Koa Middleware in ES2015
Unit Testing Express and Koa Middleware in ES2015
 
Asynchronous programming done right - Node.js
Asynchronous programming done right - Node.jsAsynchronous programming done right - Node.js
Asynchronous programming done right - Node.js
 
JS everywhere 2011
JS everywhere 2011JS everywhere 2011
JS everywhere 2011
 
Server Side Swift: Vapor
Server Side Swift: VaporServer Side Swift: Vapor
Server Side Swift: Vapor
 
Reactive programming on Android
Reactive programming on AndroidReactive programming on Android
Reactive programming on Android
 
Going fullstack React(ive) - Paulo Lopes - Codemotion Amsterdam 2017
Going fullstack React(ive) - Paulo Lopes - Codemotion Amsterdam 2017Going fullstack React(ive) - Paulo Lopes - Codemotion Amsterdam 2017
Going fullstack React(ive) - Paulo Lopes - Codemotion Amsterdam 2017
 
Distributed load testing with k6
Distributed load testing with k6Distributed load testing with k6
Distributed load testing with k6
 
Unit Testing Express Middleware
Unit Testing Express MiddlewareUnit Testing Express Middleware
Unit Testing Express Middleware
 

Recently uploaded

GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
Sri Ambati
 
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
James Anderson
 
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
Product School
 
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
Jeffrey Haguewood
 
FIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdfFIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance
 
Generating a custom Ruby SDK for your web service or Rails API using Smithy
Generating a custom Ruby SDK for your web service or Rails API using SmithyGenerating a custom Ruby SDK for your web service or Rails API using Smithy
Generating a custom Ruby SDK for your web service or Rails API using Smithy
g2nightmarescribd
 
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdfSmart TV Buyer Insights Survey 2024 by 91mobiles.pdf
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf
91mobiles
 
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Product School
 
When stars align: studies in data quality, knowledge graphs, and machine lear...
When stars align: studies in data quality, knowledge graphs, and machine lear...When stars align: studies in data quality, knowledge graphs, and machine lear...
When stars align: studies in data quality, knowledge graphs, and machine lear...
Elena Simperl
 
Bits & Pixels using AI for Good.........
Bits & Pixels using AI for Good.........Bits & Pixels using AI for Good.........
Bits & Pixels using AI for Good.........
Alison B. Lowndes
 
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Albert Hoitingh
 
UiPath Test Automation using UiPath Test Suite series, part 3
UiPath Test Automation using UiPath Test Suite series, part 3UiPath Test Automation using UiPath Test Suite series, part 3
UiPath Test Automation using UiPath Test Suite series, part 3
DianaGray10
 
JMeter webinar - integration with InfluxDB and Grafana
JMeter webinar - integration with InfluxDB and GrafanaJMeter webinar - integration with InfluxDB and Grafana
JMeter webinar - integration with InfluxDB and Grafana
RTTS
 
Connector Corner: Automate dynamic content and events by pushing a button
Connector Corner: Automate dynamic content and events by pushing a buttonConnector Corner: Automate dynamic content and events by pushing a button
Connector Corner: Automate dynamic content and events by pushing a button
DianaGray10
 
Neuro-symbolic is not enough, we need neuro-*semantic*
Neuro-symbolic is not enough, we need neuro-*semantic*Neuro-symbolic is not enough, we need neuro-*semantic*
Neuro-symbolic is not enough, we need neuro-*semantic*
Frank van Harmelen
 
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdfFIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
FIDO Alliance
 
Knowledge engineering: from people to machines and back
Knowledge engineering: from people to machines and backKnowledge engineering: from people to machines and back
Knowledge engineering: from people to machines and back
Elena Simperl
 
Leading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdfLeading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdf
OnBoard
 
PCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase TeamPCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase Team
ControlCase
 
Monitoring Java Application Security with JDK Tools and JFR Events
Monitoring Java Application Security with JDK Tools and JFR EventsMonitoring Java Application Security with JDK Tools and JFR Events
Monitoring Java Application Security with JDK Tools and JFR Events
Ana-Maria Mihalceanu
 

Recently uploaded (20)

GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
 
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
 
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
 
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
 
FIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdfFIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdf
 
Generating a custom Ruby SDK for your web service or Rails API using Smithy
Generating a custom Ruby SDK for your web service or Rails API using SmithyGenerating a custom Ruby SDK for your web service or Rails API using Smithy
Generating a custom Ruby SDK for your web service or Rails API using Smithy
 
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdfSmart TV Buyer Insights Survey 2024 by 91mobiles.pdf
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf
 
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
 
When stars align: studies in data quality, knowledge graphs, and machine lear...
When stars align: studies in data quality, knowledge graphs, and machine lear...When stars align: studies in data quality, knowledge graphs, and machine lear...
When stars align: studies in data quality, knowledge graphs, and machine lear...
 
Bits & Pixels using AI for Good.........
Bits & Pixels using AI for Good.........Bits & Pixels using AI for Good.........
Bits & Pixels using AI for Good.........
 
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
 
UiPath Test Automation using UiPath Test Suite series, part 3
UiPath Test Automation using UiPath Test Suite series, part 3UiPath Test Automation using UiPath Test Suite series, part 3
UiPath Test Automation using UiPath Test Suite series, part 3
 
JMeter webinar - integration with InfluxDB and Grafana
JMeter webinar - integration with InfluxDB and GrafanaJMeter webinar - integration with InfluxDB and Grafana
JMeter webinar - integration with InfluxDB and Grafana
 
Connector Corner: Automate dynamic content and events by pushing a button
Connector Corner: Automate dynamic content and events by pushing a buttonConnector Corner: Automate dynamic content and events by pushing a button
Connector Corner: Automate dynamic content and events by pushing a button
 
Neuro-symbolic is not enough, we need neuro-*semantic*
Neuro-symbolic is not enough, we need neuro-*semantic*Neuro-symbolic is not enough, we need neuro-*semantic*
Neuro-symbolic is not enough, we need neuro-*semantic*
 
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdfFIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
 
Knowledge engineering: from people to machines and back
Knowledge engineering: from people to machines and backKnowledge engineering: from people to machines and back
Knowledge engineering: from people to machines and back
 
Leading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdfLeading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdf
 
PCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase TeamPCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase Team
 
Monitoring Java Application Security with JDK Tools and JFR Events
Monitoring Java Application Security with JDK Tools and JFR EventsMonitoring Java Application Security with JDK Tools and JFR Events
Monitoring Java Application Security with JDK Tools and JFR Events
 

Asynchronen Code testen

  • 1. Asynchronen Code testen @ndrssmn Andreas Simon
  • 2. Synchron Übertragungszeit Wartezeit Bearbeitungszeit Wartezeit Antwortzeit
  • 4. Fehlertoleranz Verfügbarkeit Parallelisierung Performance Event-Driven Architecture
  • 6.
  • 8. Listening in JUnit @Test public void should_reply_with_Fibonacci_numbers() throws Exception { // Arrange NotificationTrace<Integer> trace = new NotificationTrace<>(TIMEOUT); String replyQueue = channel.queueDeclare().getQueue(); FibonacciCalculator.create(connection.createChannel()); new IntegerConsumer( connection.createChannel(), trace::append) .consumeQueue(replyQueue); // Act publishNumbers(MIN, MAX, replyQueue); // Assert trace.containsNotification(equalTo(FIB_MIN)); trace.containsNotification(equalTo(FIB_MAX)); }
  • 9. Listening in Mocha describe('AMQP Fibonacci service', function() { it('calculates fib(' + MIN + ')', function(done) { connection.on('ready', function () { connection.queue('my-queue', function(q) { q.subscribe(function (message) { try { // Assert message.data.toString().should.eql(FIB_MIN); done(); } catch(e) { done(e); } }); // Act connection.publish( 'calculate-fibonacci', MIN, { replyTo: 'my-queue'} ); }); }); });
  • 10. Sampling POST localhost/ CREATED Location: localhost/30 GET localhost/30 NOT FOUND GET localhost/30 NOT FOUND GET localhost/30 OK :: 832040 TIMEOUT
  • 11. Sampling in JUnit @Test public void calculates_fib_30() throws Exception { // Act connection = POST("http://localhost:3000/", "30"); fibLocation = connection.getHeaderField("Location"); // Assert Probe probe = responseTo( fibLocation, equalTo(Integer.toString(FIB_30)) ); new Poller(TIMEOUT, POLL_DELAY).check(probe); }
  • 12. public class Poller { […] public void check(Probe probe) { […] while (!probe.isSatisfied()) { […] Thread.sleep(pollDelayMillis); probe.sample(); } } } public interface Probe { void sample(); boolean isSatisfied(); void describeAcceptanceCriteriaTo(Description d); void describeFailureTo(Description d); }
  • 13. Sampling in Mocha describe('Fibonacci server', function() { it('should calculate fib(20)', function(done) { var req = http.request(POST_fib, function(res) { res.setEncoding('utf8'); res.statusCode.should.eql(201); res.headers.location.should.be.ok; pollGET(res.headers.location, done); }).on('error', done); req.setHeader('Content-Type', 'application/x-www-form- urlencoded;charset=UTF-8'); req.write('n=20n'); req.end(); });
  • 14. Sampling in Mocha function pollGET(url, done) { http.get(url, function(res) { if(200 != res.statusCode) { setTimeout(pollGET, POLL_DELAY, url, done); } res.on('data', function (chunk) { chunk.toString().should.eql('6765'); done(); }); }).on('error', done); }
  • 15. Test the test @Test public void is_thread_safe() throws Exception { latch = startStressing(STRESSING_THREADS, () -> { for (int i = 0; i < ITERATIONS; i++) { trace.append("NOT-WANTED"); Thread.sleep(SLEEPTIME); } latch.countDown(); }); scheduler.schedule( () -> trace.append("WANTED"), 100, TimeUnit.MILLISECONDS ); trace.containsNotification(equalTo("WANTED")); latch.await(); assertThat( trace.getAppendCount(), is(equalTo((long) STRESSING_THREADS * ITERATIONS + 1)) ); }
  • 16. Thread-sicher implementieren public class NotificationTrace<T> { public void append(T message) { synchronized (traceLock) { trace.add(message); traceLock.notifyAll(); } } public void containsNotification(Matcher<? super T> criteria) throws AssertionError, InterruptedException { Timeout timeout = new Timeout(timeoutMs); synchronized (traceLock) { stream = new NotificationStream<>(trace, criteria); while (!stream.hasMatched()) { if (timeout.hasTimedOut()) { throw new AssertionError(); } timeout.waitOn(traceLock); } } }
  • 17. Aufräumen @After public void tearDown() throws InterruptedException { executorService.shutdownNow(); scheduler.shutdownNow(); }
  • 18. Fazit Listening vs. Sampling Synchronisierungsmechanismen kapseln (und durch Unit-Tests validieren) Brian Goetz: "Java Concurrency in Practice" Nat Pryce, Steve Freeman: "Growing Object-Oriented Software" https://github.com/andreassimon/talk-asynchronen-code-testen
  • 19. Quagilis Andreas Simon Lazarettstr. 9 48147 Münster Fon +49 (0) 251 - 590 491 55-0 Fax +49 (0) 251 - 590 491 55-9 a.simon@quagilis.de http://www.quagilis.de Quality in Agile.