Exemplo n.º 1
0
        public static void AssertUpdateQuantity(IRealTimeInventory inventory, int toUpdate, IInventoryServiceCompletedMessage r)
        {
            Assert.True(r.Successful);
            Assert.Equal(r.RealTimeInventory.Quantity, inventory.Quantity + toUpdate);

            Assert.Equal(r.RealTimeInventory.Reserved, inventory.Reserved);
            Assert.Equal(r.RealTimeInventory.Holds, inventory.Holds);
        }
Exemplo n.º 2
0
 public async Task <StorageOperationResult> WriteInventoryAsync(IRealTimeInventory inventoryObject)
 {
     Data[inventoryObject.ProductId] = new Inventory(inventoryObject.Quantity, inventoryObject.Reserved, inventoryObject.Holds);
     return(await Task.FromResult(new StorageOperationResult()
     {
         IsSuccessful = true
     }));
 }
 private static OperationResult <IRealTimeInventory> ToOperationResult(this IRealTimeInventory realTimeInventory, bool isSuccessful)
 {
     return(new OperationResult <IRealTimeInventory>()
     {
         Data = realTimeInventory,
         IsSuccessful = isSuccessful
     });
 }
Exemplo n.º 4
0
 public Task <StorageOperationResult> WriteInventoryAsync(IRealTimeInventory inventoryObject)
 {
     _productInventories.AddOrUpdate(inventoryObject.ProductId, new RealTimeInventory(inventoryObject.ProductId, inventoryObject.Quantity, inventoryObject.Reserved, inventoryObject.Holds),
                                     (key, oldValue) => new RealTimeInventory(inventoryObject.ProductId, inventoryObject.Quantity, inventoryObject.Reserved, inventoryObject.Holds));
     return(Task.FromResult(new StorageOperationResult()
     {
         IsSuccessful = true
     }));
 }
Exemplo n.º 5
0
 public static OperationResult <IRealTimeInventory> ToSuccessOperationResult(
     this IRealTimeInventory realTimeInventory)
 {
     return(new OperationResult <IRealTimeInventory>()
     {
         Data = realTimeInventory,
         IsSuccessful = true
     });
 }
Exemplo n.º 6
0
 public static RealTimeInventoryFinalResult ProcessAndSendResult(
     this OperationResult <IRealTimeInventory> result
     , IRequestMessage requestMessage
     , Func <IRealTimeInventory, IInventoryServiceCompletedMessage> successResponseCompletedMessage
     , IRealTimeInventory realTimeInventory
     , IActorRef notificationActorRef, IPerformanceService performanceService)
 {
     return(result.ProcessAndSendResult(requestMessage, successResponseCompletedMessage, null, realTimeInventory, null, notificationActorRef, performanceService));
 }
Exemplo n.º 7
0
        public async Task <StorageOperationResult> WriteInventoryAsync(IRealTimeInventory inventoryObject)
        {
            //StorageWriteCheck.Execute(inventoryObject);

            _productInventories[inventoryObject.ProductId] = new RealTimeInventory(inventoryObject.ProductId,
                                                                                   inventoryObject.Quantity, inventoryObject.Reserved, inventoryObject.Holds);

            //  (key, oldValue) => new RealTimeInventory(inventoryObject.ProductId, inventoryObject.Quantity, inventoryObject.Reservations, inventoryObject.Holds));
            return(await Task.FromResult(new StorageOperationResult()
            {
                IsSuccessful = true
            }));
        }
Exemplo n.º 8
0
        public static Guid GenerateNextGuid(this IRealTimeInventory inventory)
        {
            var ticksAsBytes = BitConverter.GetBytes(new DateTime(1900, 1, 1).Ticks);

            Array.Reverse(ticksAsBytes);
            var increment      = Interlocked.Increment(ref GlobalEtagGeneratorCounter);
            var currentAsBytes = BitConverter.GetBytes(DateTime.UtcNow.AddHours(increment).Ticks);

            Array.Reverse(currentAsBytes);
            var bytes = new byte[16];

            Array.Copy(ticksAsBytes, 0, bytes, 0, ticksAsBytes.Length);
            Array.Copy(currentAsBytes, 0, bytes, 8, currentAsBytes.Length);
            return(new Guid(bytes));
        }
Exemplo n.º 9
0
 public static OperationResult <IRealTimeInventory> ToFailedOperationResult(
     this RealTimeInventoryException exception, IRealTimeInventory realTimeInventory, string message = null)
 {
     message = message ?? "Inventory operation failed";
     if (realTimeInventory != null)
     {
         message += GetCurrentQuantitiesReport(realTimeInventory);
     }
     exception.ErrorMessage += exception.ErrorMessage + " : " + message;
     return(new OperationResult <IRealTimeInventory>()
     {
         Data = realTimeInventory,
         IsSuccessful = false,
         Exception = exception
     });
 }
Exemplo n.º 10
0
 public static void AssertHolds(IRealTimeInventory inventory, int toHold, IInventoryServiceCompletedMessage r)
 {
     if (inventory.Quantity - inventory.Holds - toHold >= 0)
     {
         Assert.True(r.Successful);
         Assert.Equal(r.RealTimeInventory.Holds, inventory.Holds + (int)toHold);
     }
     else
     {
         Assert.False(r.Successful);
         Assert.Equal(r.RealTimeInventory.Holds, inventory.Holds);
         //-todo when there is a failure, return nothing   Assert.Equal(r.Holds, (int)toHold);
     }
     Assert.Equal(r.RealTimeInventory.Reserved, inventory.Reserved);
     Assert.Equal(r.RealTimeInventory.Quantity, inventory.Quantity);
 }
Exemplo n.º 11
0
 public static void AssertUpdateQuantityAndHold(IRealTimeInventory inventory, uint toUpdate, IInventoryServiceCompletedMessage r)
 {
     if (inventory.Holds <= inventory.Quantity)
     {
         Assert.True(r.Successful);
         Assert.Equal(r.RealTimeInventory.Quantity, inventory.Quantity + (int)toUpdate);
         Assert.Equal(r.RealTimeInventory.Reserved, inventory.Reserved);
         Assert.Equal(r.RealTimeInventory.Holds, inventory.Holds + (int)toUpdate);
     }
     else
     {
         Assert.False(r.Successful);
         Assert.Equal(r.RealTimeInventory.Quantity, inventory.Quantity);
         Assert.Equal(r.RealTimeInventory.Reserved, inventory.Reserved);
         Assert.Equal(r.RealTimeInventory.Holds, inventory.Holds);
     }
 }
Exemplo n.º 12
0
 public static void AssertPurchaseFromHolds(IRealTimeInventory inventory, uint toPurchase, IInventoryServiceCompletedMessage r)
 {
     if ((inventory.Holds >= toPurchase) && (inventory.Quantity - toPurchase >= 0))
     {
         Assert.True(r.Successful);
         Assert.Equal(r.RealTimeInventory.Quantity, inventory.Quantity - (int)toPurchase);
         Assert.Equal(r.RealTimeInventory.Holds, inventory.Holds - (int)toPurchase);
     }
     else
     {
         Assert.False(r.Successful);
         Assert.Equal(r.RealTimeInventory.Quantity, inventory.Quantity);
         Assert.Equal(r.RealTimeInventory.Holds, inventory.Holds);
         //-todo when there is a failure, return nothing      Assert.Equal(r.Quantity, (int)toPurchase);
     }
     Assert.Equal(r.RealTimeInventory.Reserved, inventory.Reserved);
 }
Exemplo n.º 13
0
        public static void AssertReservations(IRealTimeInventory initialInventory, int reservationQuantity, IInventoryServiceCompletedMessage result)
        {
            if ((initialInventory.Quantity - initialInventory.Holds - initialInventory.Reserved - reservationQuantity >= 0) || reservationQuantity <= 0)
            {
                Assert.True(result.Successful);

                var newReserved = Math.Max(0, initialInventory.Reserved + reservationQuantity);

                Assert.Equal(result.RealTimeInventory.Reserved, newReserved);
            }
            else
            {
                Assert.False(result.Successful);
                Assert.Equal(result.RealTimeInventory.Reserved, initialInventory.Reserved);
            }
            Assert.Equal(result.RealTimeInventory.Holds, initialInventory.Holds);
            Assert.Equal(result.RealTimeInventory.Quantity, initialInventory.Quantity);
        }
 public ResetInventoryQuantityReserveAndHoldCompletedMessage(IRealTimeInventory realTimeInventory, bool successful)
 {
     RealTimeInventory = realTimeInventory;
     Successful        = successful;
 }
        public static RealTimeInventoryException Generate(ErrorType errorType, IRealTimeInventory currentRealTimeInventory, int requestUpdate, Exception exceptionThrown = null)
        {
            string result;

            switch (errorType)
            {
            case ErrorType.Unknown:
                result = "Unknown";
                break;

            case ErrorType.NO_PRODUCT_ID_SPECIFIED:

                result = "No product ID specified";

                break;

            case ErrorType.UNABLE_TO_READ_INV:
                result = "Unable to read inventory for product " + currentRealTimeInventory.ProductId;

                break;

            case ErrorType.RESERVATION_EXCEED_QUANTITY:
                result = "What to reserve must be less than quantity - reservation for product " +
                         currentRealTimeInventory.ProductId;

                break;

            case ErrorType.UNABLE_TO_UPDATE_INVENTORY_STORAGE:
                result = "Unable to update inventory storage for product " +
                         currentRealTimeInventory.ProductId + " with quantity " + requestUpdate;

                break;

            case ErrorType.HOLD_EXCEED_QUANTITY_FOR_HOLD:

                result = "Unable to  hold With the supplied hold " + requestUpdate +
                         ",the new hold is larger than resulting quantity for product " +
                         currentRealTimeInventory.ProductId;

                break;

            case ErrorType.HOLD_EXCEED_QUANTITY_FOR_UPDATEQUANTITYANDHOLD:

                result = "Unable to update quantity and hold With the supplied hold " + requestUpdate +
                         ",the new hold is larger than resulting quantity for product " +
                         currentRealTimeInventory.ProductId;

                break;

            case ErrorType.NEGATIVE_PURCHASE_FOR_PURCHASEFROMRESERVATION:
                result = "Cannot purchase from reservation with a negative quantity of " + requestUpdate + " for product " +
                         currentRealTimeInventory.ProductId;

                break;

            case ErrorType.PURCHASE_EXCEED_QUANTITY_FOR_PURCHASEFROMRESERVATION:
                result = "provided " + requestUpdate +
                         ", available holds must be less than or equal to quantity for product " +
                         currentRealTimeInventory.ProductId;
                break;

            case ErrorType.PURCHASE_EXCEED_QUANTITY_FOR_PURCHASEFROMHOLD:
                result = "provided " + requestUpdate +
                         " for purchase from hold available holds must be less than or equal to quantity for product " +
                         currentRealTimeInventory.ProductId;

                break;

            case ErrorType.NEGATIVE_PURCHASE_FOR_PURCHASEFROMHOLD:
                result = "Cannot purchase from hold with a negative quantity of " + requestUpdate + " for product " +
                         currentRealTimeInventory.ProductId;
                break;

            default:
                result = "Unknown";
                break;
            }

            var time = DateTime.UtcNow;

            result = errorType.ToString() + " with requested update quantity " + requestUpdate + " : " + result + (exceptionThrown != null ? " - " + exceptionThrown.Message + " - " + exceptionThrown.InnerException?.Message : "") + " At " + time;
            return(new RealTimeInventoryException()
            {
                ErrorMessage = result
            });// RealTimeInventoryException(result, errorType, time, exceptionThrown);
        }
Exemplo n.º 16
0
 public static bool IsMostRecentThan(this IRealTimeInventory @thisRealTimeInventory, IRealTimeInventory comparedToRealTimeInventory)
 {
     return(thisRealTimeInventory.ETag != null && thisRealTimeInventory.ETag.IsMostRecentThan(comparedToRealTimeInventory.ETag));
 }
Exemplo n.º 17
0
 public static BigInteger ToEtagComparable(this IRealTimeInventory inventory)
 {
     return(inventory.ETag.ToEtagComparable());
 }
Exemplo n.º 18
0
 public static string GetCurrentQuantitiesReport(this IRealTimeInventory realTimeInventory)
 {
     return(" [ product : " + realTimeInventory.ProductId + " / quantity : " + realTimeInventory.Quantity + " / reservations: " + realTimeInventory.Reserved + " / holds: " + realTimeInventory.Holds + "  / etag: " + realTimeInventory.ETag + " ]");
 }
Exemplo n.º 19
0
 public RealTimeInventoryChangeMessage(IRealTimeInventory realTimeInventory)
 {
     RealTimeInventory = realTimeInventory;
 }
        public GetInventoryCompletedMessage(IRealTimeInventory realTimeInventory, bool successful)
        {
            Successful = successful;

            RealTimeInventory = realTimeInventory;
        }
        public static async Task <OperationResult <IRealTimeInventory> > ResetInventoryQuantityReserveAndHoldAsync(this IRealTimeInventory currentInventory, IInventoryStorage inventoryStorage, string productId, int quantity, int reserve, int hold)
        {
            IRealTimeInventory realTimeInventory = new RealTimeInventory(currentInventory.ProductId, 0, 0, 0);

            var result = await realTimeInventory.UpdateQuantityAsync(inventoryStorage, productId, quantity);

            if (result.IsSuccessful)
            {
                result = await result.Data.ReserveAsync(inventoryStorage, productId, reserve);

                if (result.IsSuccessful)
                {
                    result = await result.Data.PlaceHoldAsync(inventoryStorage, productId, hold);

                    if (result.IsSuccessful)
                    {
                        return(result.Data.ToOperationResult(isSuccessful: true));
                    }
                }
            }
            result.Data = currentInventory;
            return(result);
        }
        public static async Task <OperationResult <IRealTimeInventory> > UpdateQuantityAsync(this IRealTimeInventory realTimeInventory, IInventoryStorage inventoryStorage, string productId, int quantity)
        {
            var newQuantity = realTimeInventory.Quantity + quantity;

            var newRealTimeInventory = new RealTimeInventory(productId, newQuantity, realTimeInventory.Reserved, realTimeInventory.Holds);
            var result = await inventoryStorage.WriteInventoryAsync(newRealTimeInventory);

            if (!result.IsSuccessful)
            {
                return(InventoryServiceErrorMessageGenerator.Generate(ErrorType.UNABLE_TO_UPDATE_INVENTORY_STORAGE, realTimeInventory, quantity, result.Errors).ToFailedOperationResult(realTimeInventory, productId));
            }

            return(newRealTimeInventory.ToOperationResult(isSuccessful: true));
        }
        public static async Task <OperationResult <IRealTimeInventory> > InventoryStorageFlushAsync(this IRealTimeInventory realTimeInventory, IInventoryStorage inventoryStorage, string id)
        {
            await inventoryStorage.FlushAsync();

            return(realTimeInventory.ToOperationResult(isSuccessful: true));
        }
Exemplo n.º 24
0
        public async Task <IInventoryServiceCompletedMessage> PerformOperation(IRequestMessage request, Task <OperationResult <IRealTimeInventory> > response, IRealTimeInventory originalInventory)
        {
            var perf = new TestPerformanceService();

            perf.Init();
            return((await response).ProcessAndSendResult(request, CompletedMessageFactory.GetResponseCompletedMessage(request), originalInventory, null, perf).InventoryServiceCompletedMessage);
        }
        public static async Task <OperationResult <IRealTimeInventory> > ReserveAsync(this IRealTimeInventory realTimeInventory, IInventoryStorage inventoryStorage, string productId, int reservationQuantity)
        {
            var newReserved = Math.Max(0, realTimeInventory.Reserved + reservationQuantity);

            if ((reservationQuantity > 0) && (newReserved > realTimeInventory.Quantity - realTimeInventory.Holds))
            {
                return(InventoryServiceErrorMessageGenerator.Generate(ErrorType.RESERVATION_EXCEED_QUANTITY, realTimeInventory, reservationQuantity).ToFailedOperationResult(realTimeInventory, productId));
            }

            var newRealTimeInventory = new RealTimeInventory(productId, realTimeInventory.Quantity, newReserved, realTimeInventory.Holds);

            var result = await inventoryStorage.WriteInventoryAsync(newRealTimeInventory);

            if (!result.IsSuccessful)
            {
                return(InventoryServiceErrorMessageGenerator.Generate(ErrorType.UNABLE_TO_UPDATE_INVENTORY_STORAGE, realTimeInventory, reservationQuantity, result.Errors).ToFailedOperationResult(realTimeInventory, productId));
            }

            return(newRealTimeInventory.ToOperationResult(isSuccessful: true));
        }
Exemplo n.º 26
0
        public UpdateQuantityCompletedMessage(IRealTimeInventory realTimeInventory, bool successful)
        {
            Successful = successful;

            RealTimeInventory = realTimeInventory;
        }
        public PurchaseFromHoldsCompletedMessage(IRealTimeInventory realTimeInventory, bool successful)
        {
            Successful = successful;

            RealTimeInventory = realTimeInventory;
        }
 public InventoryOperationErrorMessage(IRealTimeInventory realTimeInventory, RealTimeInventoryException error)
 {
     Error             = error;
     RealTimeInventory = realTimeInventory;
     Successful        = false;
 }
Exemplo n.º 29
0
        public static RealTimeInventoryFinalResult ProcessAndSendResult(this OperationResult <IRealTimeInventory> result, IRequestMessage requestMessage, Func <IRealTimeInventory, IInventoryServiceCompletedMessage> successResponseCompletedMessage, ILoggingAdapter logger, IRealTimeInventory realTimeInventory, IActorRef sender, IActorRef notificationActorRef, IPerformanceService performanceService)
        {
            logger?.Info(requestMessage.GetType().Name + " Request was " + (!result.IsSuccessful ? " NOT " : "") + " successful.  Current Inventory :  " + realTimeInventory.GetCurrentQuantitiesReport());

            IInventoryServiceCompletedMessage response;

            if (!result.IsSuccessful)
            {
                response = result.ToInventoryOperationErrorMessage(requestMessage.ProductId);
                var message = "Error while trying to " + requestMessage.GetType() + " - The sender of the message is " + sender?.Path + result.Exception.ErrorMessage;
                notificationActorRef?.Tell(message);
                logger?.Error(message, requestMessage, result, realTimeInventory.GetCurrentQuantitiesReport());
            }
            else
            {
                realTimeInventory = result.Data as RealTimeInventory;
                response          = successResponseCompletedMessage(realTimeInventory);
                logger?.Info(response.GetType().Name + " Response was sent back. Current Inventory : " + realTimeInventory.GetCurrentQuantitiesReport() + " - The sender of the message is " + sender.Path);
            }
            sender?.Tell(response);
            notificationActorRef?.Tell(new RealTimeInventoryChangeMessage(realTimeInventory));
            performanceService?.Increment("Completed " + requestMessage.GetType().Name);
            return(new RealTimeInventoryFinalResult(realTimeInventory as RealTimeInventory, response, result));
        }
 public UpdateAndHoldQuantityCompletedMessage(IRealTimeInventory realTimeInventory, bool successful)
 {
     RealTimeInventory = realTimeInventory;
     Successful        = successful;
 }