Created
July 17, 2021 06:47
-
-
Save marcellodesales/39857e464b38167f0631150ee6238943 to your computer and use it in GitHub Desktop.
The example implementing the execution of concurrent Future async calls in dart synchronized by the Google's Tuple API, as described at https://stackoverflow.com/questions/42176092/dartlang-wait-more-than-one-future/67736970#67736970
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Fetch the ticket status with fee associated with it | |
// 2 future calls are made concurrently, so that it will be constructing the status object properly | |
static Future<TicketStatus> fetchTicketStatusWithFee({ required String uid, required int marketPlaceId, | |
required int storeId, required String ticketId }) async { | |
// https://stackoverflow.com/questions/42176092/dartlang-wait-more-than-one-future/67736970#67736970 | |
// Calling both endpoints and sychronizing on them | |
final tupleTicketStatusAndFee = await waitConcurrently<TicketStatus, double>( | |
// Future<TicketSTatus> is returned on item1 | |
fetchTicketStatus(uid: uid, marketPlaceId: marketPlaceId, storeId: storeId, ticketId: ticketId), | |
// Future<double> is returned on item 2 | |
getServiceFee(uid: uid, marketPlaceId: marketPlaceId, storeId: storeId) | |
); | |
// Set the ticket status after getting it | |
TicketStatus ticketStatus = tupleTicketStatusAndFee.item1; | |
ticketStatus.fee = tupleTicketStatusAndFee.item2; | |
return ticketStatus; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ dart test test/parking_lot/ticket_status_remote_repository_existing_full_test.dart | |
00:02 +0: Given the ticket id that exists the call must return an exception | |
┌─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────── | |
│ #0 ParkingTicketsRemoteRepository.fetchTicketStatus (package:xxxxx_platform_client/cash/_super/platform/parking_lot/parking_ticket_remote_repository.dart:42:13) | |
│ #1 ParkingTicketsRemoteRepository.fetchTicketStatusWithFee (package:xxxxxxx_platform_client/cash/_super/platform/parking_lot/parking_ticket_remote_repository.dart:26:9) | |
├┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄ | |
│ 🐛 Fetching the ticket status from xxxxx at https://xxxxxxxx/v2/parking_lot/tickets/327177824178 | |
└─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────── | |
┌─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────── | |
│ #0 ParkingTicketsRemoteRepository.getServiceFee (package:xxxxx_platform_client/cash/_super/platform/parking_lot/parking_ticket_remote_repository.dart:74:13) | |
│ #1 ParkingTicketsRemoteRepository.fetchTicketStatusWithFee (package:xxxxx_platform_client/cash/_super/platform/parking_lot/parking_ticket_remote_repository.dart:27:9) | |
├┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄ | |
│ 🐛 Fetching the payment service fee at https://xxxxxxx/v2/payment/servicefee | |
└─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────── | |
╔╣ Request ║ GET | |
║ https://xxxxxx/v2/parking_lot/tickets/327177824178 | |
╚══════════════════════════════════════════════════════════════════════════════════════════╝ | |
╔ Headers | |
╟ X-xxxx-Tid: a84c3c1c-78d2-4e23-80ad-94742ef56ab3 | |
╟ X-xxxxx-Uid: 1234567 | |
╟ X-xxxx-MarketplaceId: yyyyy | |
╟ X-xxxx-App-Version: 1.0 | |
╟ X-xxxxx-StoreId: 1 | |
╟ followRedirects: true | |
╟ connectTimeout: 5000 | |
╟ receiveTimeout: 3000 | |
╚══════════════════════════════════════════════════════════════════════════════════════════╝ | |
╔╣ Request ║ GET | |
║ https://xxxxxxx/v2/parking_lot/payment/servicefee | |
╚══════════════════════════════════════════════════════════════════════════════════════════╝ | |
╔ Headers | |
╟ X-xxxxxx-Tid: e4373e14-352e-48c1-8642-e778e8f20b20 | |
╟ X-xxxxx-Uid: yy | |
╟ X-xxxxxx-MarketplaceId: yyy | |
╟ X-xxxxx-App-Version: 1.0 | |
╟ X-xxxxx-StoreId: 1 | |
╟ followRedirects: true | |
╟ connectTimeout: 5000 | |
╟ receiveTimeout: 3000 | |
╚══════════════════════════════════════════════════════════════════════════════════════════╝ | |
╔╣ Response ║ GET ║ Status: 200 | |
║ https://xxxxxxxx/v2/parking_lot/payment/servicefee | |
╚══════════════════════════════════════════════════════════════════════════════════════════╝ | |
╔ Headers | |
╟ connection: [close] | |
╟ date: [Sat, 17 Jul 2021 06:35:07 GMT] | |
╟ vary: [Origin, Access-Control-Request-Method, Access-Control-Request-Headers] | |
╟ x-xxxxxxx-api-version: [v2] | |
╟ content-type: [application/json] | |
╟ x-xxxxxxx-build-version: [3c5cf42e-develop] | |
╟ server: [nginx/1.19.6] | |
╟ x-b3-traceid: [c8bb963ab935f6a2] | |
╚══════════════════════════════════════════════════════════════════════════════════════════╝ | |
╔ Body | |
║ | |
║ { | |
║ service_fee: 80 | |
║ } | |
║ | |
╚══════════════════════════════════════════════════════════════════════════════════════════╝ | |
┌─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────── | |
│ #0 ParkingTicketsRemoteRepository.getServiceFee (package:xxxxx_platform_client/cash/_super/platform/parking_lot/parking_ticket_remote_repository.dart:95:13) | |
│ #1 <asynchronous suspension> | |
├┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄ | |
│ 🐛 service fee: 0.8 | |
└─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────── | |
╔╣ Response ║ GET ║ Status: 200 | |
║ https://api.super.cash/v2/parking_lot/tickets/327177824178 | |
╚══════════════════════════════════════════════════════════════════════════════════════════╝ | |
╔ Headers | |
╟ connection: [close] | |
╟ date: [Sat, 17 Jul 2021 06:35:08 GMT] | |
╟ vary: [Origin, Access-Control-Request-Method, Access-Control-Request-Headers] | |
╟ x-xxxxxx-api-version: [v2] | |
╟ content-type: [application/json] | |
╟ x-xxxx-build-version: [3c5cf42e-develop] | |
╟ server: [nginx/1.19.6] | |
╟ x-b3-traceid: [8160099eae9492b2] | |
╚══════════════════════════════════════════════════════════════════════════════════════════╝ | |
╔ Body | |
║ | |
║ { | |
║ status: { | |
║ cnpjGaragem: "12.200.135/0001-80", | |
║ dataConsulta: 1626503708310, | |
║ dataDeEntrada: 1626468337000, | |
║ dataPermitidaSaida: 1626504300000, | |
║ dataPermitidaSaidaUltimoPagamento: null, | |
║ errorCode: 0, | |
║ garagem: "xxxx SHOPPING", | |
║ idGaragem: 1, | |
║ idPromocao: null, | |
║ imagemLink: null, | |
║ mensagem: "", | |
║ notas: [ | |
║ ], | |
║ numeroTicket: "327177824178", | |
║ promocaoAtingida: false, | |
║ promocoesDisponiveis: false, | |
║ setor: "ESTACIONAMENTO", | |
║ tarifa: 1700, | |
║ tarifaPaga: 0, | |
║ tarifaSemDesconto: 1700, | |
║ ticketValido: true, | |
║ valorDesconto: 0 | |
║ } | |
║ ticketStatus: "NOT_PAID" | |
║ } | |
║ | |
╚══════════════════════════════════════════════════════════════════════════════════════════╝ | |
┌─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────── | |
│ #0 main.<anonymous closure> (file:///Users/marcellodesales/dev/gitlab.com/xxxxxx/clients/xxxxx-platform-client-dart/test/parking_lot/ticket_status_remote_repository_existing_full_test.dart:62:14) | |
│ #1 <asynchronous suspension> | |
├┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄ | |
│ 🐛 Ticket Status for ticketId=327177824178: TicketStatus: [ticketId: 327177824178] | |
└─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────── | |
00:03 +1: All tests passed! |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment