@@ -23,9 +23,118 @@ API documentation is available http://rawgit.davedoesdev.com/davedoesdev/atribut
23
23
24
24
[source,javascript]
25
25
----
26
- include::test/example.js[]
26
+ const { Atributo } = require('atributo'),
27
+ async = require('async'),
28
+ assert = require('assert');
29
+
30
+ // Open the database file
31
+ new Atributo({ db_filename: 'atributo.sqlite3' }).on('ready', function () {
32
+ async.waterfall([
33
+
34
+ // Make instances available
35
+ cb => this.available('instance0', cb),
36
+ cb => this.available('instance1', cb),
37
+
38
+ // List instances
39
+ cb => this.instances(cb),
40
+ (instances, cb) => {
41
+ assert.deepStrictEqual(instances, [
42
+ { id: 'instance0', available: true },
43
+ { id: 'instance1', available: true }
44
+ ]);
45
+ cb();
46
+ },
47
+
48
+ // Allocate jobs
49
+ cb => this.allocate('job0', cb),
50
+ (persisted, instance_id, cb) => {
51
+ assert(persisted); // <1>
52
+ assert.strictEqual(instance_id, 'instance1');
53
+ cb();
54
+ },
55
+ cb => this.allocate('job1', cb),
56
+ (persisted, instance_id, cb) => {
57
+ assert(persisted); // <1>
58
+ assert.strictEqual(instance_id, 'instance0');
59
+ cb();
60
+ },
61
+
62
+ // List jobs for each instance
63
+ cb => this.jobs('instance0', cb),
64
+ (jobs, cb) => {
65
+ assert.deepStrictEqual(jobs, ['job1']);
66
+ cb();
67
+ },
68
+ cb => this.jobs('instance1', cb),
69
+ (jobs, cb) => {
70
+ assert.deepStrictEqual(jobs, ['job0']);
71
+ cb();
72
+ },
73
+
74
+ // Check if instance has jobs
75
+ cb => this.has_jobs('instance0', cb),
76
+ (has_jobs, cb) => {
77
+ assert(has_jobs);
78
+ cb();
79
+ },
80
+
81
+ // Get instance for job
82
+ cb => this.instance('job1', cb),
83
+ (instance_id, cb) => {
84
+ assert.strictEqual(instance_id, 'instance0');
85
+ cb();
86
+ },
87
+
88
+ // Make instance unavailable but don't remove it
89
+ cb => this.unavailable('instance0', false, cb),
90
+
91
+ // Check instance is unavailable
92
+ cb => this.instances(cb),
93
+ (instances, cb) => {
94
+ assert.deepStrictEqual(instances, [
95
+ { id: 'instance0', available: false },
96
+ { id: 'instance1', available: true }
97
+ ]);
98
+ cb();
99
+ },
100
+
101
+ // Check existing allocation to unavailable instance
102
+ cb => this.allocate('job1', cb),
103
+ (persisted, instance_id, cb) => {
104
+ assert(!persisted); // <2>
105
+ assert.strictEqual(instance_id, 'instance0');
106
+ cb();
107
+ },
108
+
109
+ // Deallocate existing allocation
110
+ cb => this.deallocate('job1', cb), // <3>
111
+
112
+ // Re-allocate job
113
+ cb => this.allocate('job1', cb),
114
+ (persisted, instance_id, cb) => {
115
+ assert(persisted); // <1>
116
+ assert.strictEqual(instance_id, 'instance1');
117
+ cb();
118
+ },
119
+
120
+ // Remove instance and its allocated jobs
121
+ cb => this.unavailable('instance0', true, cb),
122
+
123
+ // Check instance has been removed
124
+ cb => this.instances(cb),
125
+ (instances, cb) => {
126
+ assert.deepStrictEqual(instances, [
127
+ { id: 'instance1', available: true }
128
+ ]);
129
+ cb();
130
+ },
131
+
132
+ // Close database
133
+ cb => this.close(cb)
134
+
135
+ ], assert.ifError);
136
+ });
27
137
----
28
-
29
138
<1> This is a new allocation persisted to the database in this call.
30
139
<2> This is an allocation which already existed in the database before the
31
140
instance was made unavailable.
0 commit comments