File Coverage

blib/lib/Dancer2/Plugin/Queue/Role/Test.pm
Criterion Covered Total %
statement 28 40 70.0
branch n/a
condition n/a
subroutine 10 12 83.3
pod n/a
total 38 52 73.0


line stmt bran cond sub pod time code
1 1     1   7419 use 5.008001;
  1         2  
2 1     1   4 use strict;
  1         1  
  1         16  
3 1     1   3 use warnings;
  1         2  
  1         54  
4              
5             package Dancer2::Plugin::Queue::Role::Test;
6             # ABSTRACT: A Test::Roo::Role for testing Queue backends
7              
8             our $VERSION = '0.006';
9              
10 1     1   461 use Test::Roo::Role;
  1         446  
  1         4  
11 1     1   751 use MooX::Types::MooseLike::Base qw/Str HashRef CodeRef/;
  1         1  
  1         56  
12              
13 1     1   598 use HTTP::Tiny;
  1         35340  
  1         37  
14 1     1   501 use Test::TCP;
  1         30879  
  1         150  
15              
16             #pod =attr backend
17             #pod
18             #pod The short name for a Dancer2::Plugin::Queue implementation to test.
19             #pod
20             #pod =cut
21              
22             has backend => (
23             is => 'ro',
24             isa => Str,
25             required => 1,
26             );
27              
28             #pod =attr options
29             #pod
30             #pod A hash reference of options to configure the backend.
31             #pod
32             #pod =cut
33              
34             has options => (
35             is => 'lazy',
36             isa => HashRef,
37             );
38              
39       0     sub _build_options { }
40              
41             has _server => (
42             is => 'lazy',
43             isa => CodeRef,
44             );
45              
46             sub _build__server {
47 1     1   8 my ($self) = @_;
48             return sub {
49             package
50             MyServer;
51 1     1   406 use Dancer2 ':syntax';
  1         386700  
  1         6  
52 1     1   56802 use Dancer2::Plugin::Queue;
  1         3  
  1         10  
53 0     0     my $port = shift;
54              
55 0           set confdir => '.';
56 0           set startup_info => 0;
57 0           set show_errors => 1;
58 0           set plugins => {
59             Queue => {
60             default => {
61             class => $self->backend,
62             options => $self->options,
63             },
64             }
65             };
66              
67             get '/add' => sub {
68 0           queue->add_msg( params->{msg} );
69 0           my ( $msg, $body ) = queue->get_msg;
70 0           queue->remove_msg($msg);
71 0           return $body;
72 0           };
73              
74 0           Dancer2->runner->server->{ port } = $port;
75 0           start;
76 1         16 };
77             }
78              
79             test 'queue and dequeue' => sub {
80             my $self = shift;
81             test_tcp(
82             client => sub {
83             my $port = shift;
84             my $url = "http://localhost:$port/";
85              
86             my $ua = HTTP::Tiny->new;
87              
88             my $res = $ua->get( $url . "add?msg=Hello%20World" );
89             like $res->{content}, qr/Hello World/i, "sent and receieved message";
90             },
91             server => $self->_server,
92             );
93             };
94              
95             1;
96              
97              
98             # vim: ts=4 sts=4 sw=4 et:
99              
100             __END__
101              
102             =pod
103              
104             =encoding UTF-8
105              
106             =head1 NAME
107              
108             Dancer2::Plugin::Queue::Role::Test - A Test::Roo::Role for testing Queue backends
109              
110             =head1 VERSION
111              
112             version 0.006
113              
114             =head1 SYNOPSIS
115              
116             In your backend test file:
117              
118             use Test::Roo;
119              
120             with 'Dancer2::Plugin::Queue::Role::Test';
121              
122             run_me({ backend => 'Array', options => { name => "foo" } });
123              
124             done_testing;
125              
126             =head1 DESCRIPTION
127              
128             This module is a L<Test::Roo> role used for testing L<Dancer2::Plugin::Queue>
129             implementations.
130              
131             Implementations using this test role must ensure that the following modules are
132             included as test requirements:
133              
134             =over 4
135              
136             =item *
137              
138             L<HTTP::Tiny>
139              
140             =item *
141              
142             L<Test::Roo>
143              
144             =item *
145              
146             L<Test::TCP>
147              
148             =back
149              
150             =head1 ATTRIBUTES
151              
152             =head2 backend
153              
154             The short name for a Dancer2::Plugin::Queue implementation to test.
155              
156             =head2 options
157              
158             A hash reference of options to configure the backend.
159              
160             =for Pod::Coverage method_names_here
161              
162             =head1 AUTHOR
163              
164             David Golden <dagolden@cpan.org>
165              
166             =head1 COPYRIGHT AND LICENSE
167              
168             This software is Copyright (c) 2012 by David Golden.
169              
170             This is free software, licensed under:
171              
172             The Apache License, Version 2.0, January 2004
173              
174             =cut