Fixing GetCart for non-existing user

Now GetCart returns an empty cart for non-existing user
(user that haven't added anything to the cart before)
Added test to cover that

Also enhanced windows script for running cart service locally.
Now we have two options:
 - Running the service and redis locally (assuming redis emulator is installed)
   This is good for easier local debugging and troubleshooting

   docker_setup local

 - Running the service and redis in two separate docker containers
   This is good for better simulation of what will happen in the cloud
This commit is contained in:
Simon Zeltser 2018-06-26 14:41:00 -07:00
parent 289bd4db13
commit 2041bdcb48
3 changed files with 64 additions and 12 deletions

View file

@ -69,13 +69,13 @@ namespace cartservice.cartstore
// Access the cart from the cache
var value = await db.HashGetAsync(userId, CART_FIELD_NAME);
Hipstershop.Cart cart = null;
if (!value.IsNull)
{
cart = Hipstershop.Cart.Parser.ParseFrom(value);
return Hipstershop.Cart.Parser.ParseFrom(value);
}
return cart;
// We decided to return empty cart in cases when user wasn't in the cache before
return new Hipstershop.Cart();
}
}
}

View file

@ -1,16 +1,45 @@
@echo off
set ENV=%1
IF %ENV%==local GOTO local
IF %ENV%==docker GOTO docker_local
GOTO End1
:local
set REDIS_PORT=6379
set REDIS_ADDR=172.30.147.193
set REDIS_ADDR=localhost:%REDIS_PORT%
set CART_SERVICE_ADDR=127.0.0.1
set CART_SERVICE_PORT=7070
rem run docker container with redis
rem docker run -d --name=redis -p %REDIS_PORT%:%REDIS_PORT% redis:alpine
echo running redis emulator locally on a separate window
taskkill /f /im "redis-server.exe"
start redis-server
rem running locally
echo running the cart service locally
dotnet build ..\.
dotnet run --project ../cartservice.csproj start
GOTO End1
:docker_local
set REDIS_PORT=6379
set REDIS_ADDR=redis:%REDIS_PORT%
set CART_SERVICE_ADDR=127.0.0.1
set CART_SERVICE_PORT=7070
echo run docker container with redis
docker run -d --name=redis -p %REDIS_PORT%:%REDIS_PORT% redis
echo building container image for cart service
docker build -t cartservice ..\.
echo run container image for cart service
docker run -it --rm -e REDIS_ADDR=%REDIS_ADDR% -e CART_SERVICE_ADDR=%CART_SERVICE_ADDR% -e CART_SERVICE_PORT=%CART_SERVICE_PORT% -p %CART_SERVICE_PORT%:%CART_SERVICE_PORT% cartservice
GOTO End1
:End1
rem run docker container with cart service
rem docker run -it --rm -e REDIS_ADDR=%REDIS_ADDR%:%REDIS_PORT% -e CART_SERVICE_ADDR=%CART_SERVICE_ADDR% -e CART_SERVICE_PORT=%CART_SERVICE_PORT% -p %CART_SERVICE_PORT%:%CART_SERVICE_PORT% cartservice
rem -e GRPC_TRACE=all -e GRPC_VERBOSITY=debug

View file

@ -12,6 +12,29 @@ namespace cartservice
private static string serverHostName = "localhost";
private static int port = 7070;
[Fact]
public async Task GetItem_NoAddItemBefore_EmptyCartReturned()
{
string userId = Guid.NewGuid().ToString();
// Construct server's Uri
string targetUri = $"{serverHostName}:{port}";
// Create a GRPC communication channel between the client and the server
var channel = new Channel(targetUri, ChannelCredentials.Insecure);
var client = new CartServiceClient(channel);
var request = new GetCartRequest
{
UserId = userId,
};
var cart = await client.GetCartAsync(request);
Assert.NotNull(cart);
// All grpc objects implement IEquitable, so we can compare equality with by-value semantics
Assert.Equal(new Cart(), cart);
}
[Fact]
public async Task AddItem_ItemInserted()
{