Debian Codegraph @

t/integration/resource/Resource.pm

raw
1 package Resource;
2 use strict;
3 use warnings;
4
5 use parent 'Test2::Harness::Runner::Resource';
6
7 my $limit = 2;
8
9 my $no_slots_msg = 0;
10 sub available {
11 my $self = shift;
12 my ($task) = @_;
13
14 for my $slot (1 .. $limit) {
15 return 1 unless defined $self->{$slot};
16 }
17
18 $self->message("No Slots") unless $no_slots_msg++;
19 return 0;
20 }
21
22 sub assign {
23 my $self = shift;
24 my ($task, $state) = @_;
25
26 for my $slot (1 .. $limit) {
27 next if defined $self->{$slot};
28
29 $self->message("Assigned: $task->{job_id} - $slot");
30 $state->{record} = $slot;
31 $state->{env_vars}->{RESOURCE_TEST} = $slot;
32 push @{$state->{args}} => $slot;
33
34 return;
35 }
36
37 die "Error, no slots to assign";
38 }
39
40 sub record {
41 my $self = shift;
42 my ($job_id, $slot) = @_;
43
44 $self->message("Record: $job_id - $slot");
45 $self->{$slot} = $job_id;
46 $self->{$job_id} = $slot;
47 }
48
49 sub release {
50 my $self = shift;
51 my ($job_id) = @_;
52
53 my $slot = delete $self->{$job_id};
54 delete $self->{$slot};
55 $self->message("Release: $job_id - $slot");
56 }
57
58 sub cleanup {
59 my $self = shift;
60
61 $self->message("RESOURCE CLEANUP");
62 }
63
64 my $pid;
65 sub message {
66 my $self = shift;
67 my ($msg) = @_;
68
69 if (!$pid || $$ != $pid) {
70 $pid = $$;
71
72 print "$$ - $0\n";
73 }
74
75 print "$$ - $msg\n";
76 }
77
78 1;