|
1 | | -// |
2 | | -// File.swift |
3 | | -// |
4 | | -// |
5 | | -// Created by Jimmy McDermott on 10/10/20. |
6 | | -// |
| 1 | +import QueuesDatabaseHooks |
| 2 | +import Queues |
| 3 | +import XCTQueues |
| 4 | +import XCTVapor |
| 5 | +import Fluent |
| 6 | +import FluentSQLiteDriver |
| 7 | +import XCTest |
7 | 8 |
|
8 | | -import Foundation |
| 9 | +final class QueuesDatabaseHooksTests: XCTestCase { |
| 10 | + var app: Application! |
| 11 | + |
| 12 | + override func setUpWithError() throws { |
| 13 | + self.app = Application(.testing) |
| 14 | + self.app.databases.use(.sqlite(.memory, maxConnectionsPerEventLoop: 1), as: .sqlite) |
| 15 | + self.app.migrations.add(QueueDatabaseEntryMigration()) |
| 16 | + try self.app.migrator.setupIfNeeded().wait() |
| 17 | + try self.app.migrator.prepareBatch().wait() |
| 18 | + self.app.queues.use(.test) |
| 19 | + self.app.queues.add(QueuesDatabaseNotificationHook(db: self.app.db, errorClosure: { $0.localizedDescription }, payloadClosure: { $0 })) |
| 20 | + } |
| 21 | + |
| 22 | + override func tearDownWithError() throws { |
| 23 | + self.app.shutdown() |
| 24 | + } |
| 25 | + |
| 26 | + func testJobLifecycle() throws { |
| 27 | + struct Foo: Job { |
| 28 | + struct Payload: Codable {} |
| 29 | + |
| 30 | + func dequeue(_ context: QueueContext, _: Payload) -> EventLoopFuture<Void> { |
| 31 | + context.eventLoop.makeSucceededVoidFuture() |
| 32 | + } |
| 33 | + |
| 34 | + func error(_ context: QueueContext, _: Error, _: Payload) -> EventLoopFuture<Void> { |
| 35 | + context.eventLoop.makeSucceededVoidFuture() |
| 36 | + } |
| 37 | + } |
| 38 | + |
| 39 | + self.app.queues.add(Foo()) |
| 40 | + self.app.get("foo") { $0.queue.dispatch(Foo.self, .init()).map { _ in "done" } } |
| 41 | + try app.testable().test(.GET, "/foo") { XCTAssertEqual($0.body.string, "done") } |
| 42 | + |
| 43 | + let entries1 = try QueueDatabaseEntry.query(on: self.app.db).all().wait() |
| 44 | + XCTAssertEqual(entries1.count, 1) |
| 45 | + XCTAssertEqual(entries1.first?.status, .queued) |
| 46 | + XCTAssertNil(entries1.first?.dequeuedAt) |
| 47 | + XCTAssertNil(entries1.first?.completedAt) |
| 48 | + XCTAssertNil(entries1.first?.errorString) |
| 49 | + |
| 50 | + try app.queues.queue.worker.run().wait() |
| 51 | + |
| 52 | + let entries2 = try QueueDatabaseEntry.query(on: self.app.db).all().wait() |
| 53 | + XCTAssertEqual(entries2.count, 1) |
| 54 | + XCTAssertEqual(entries2.first?.status, .success) |
| 55 | + XCTAssertNotNil(entries2.first?.dequeuedAt) |
| 56 | + XCTAssertNotNil(entries2.first?.completedAt) |
| 57 | + XCTAssertNil(entries2.first?.errorString) |
| 58 | + } |
| 59 | +} |
0 commit comments