Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Recommendation service e2e testing #198

Merged
merged 9 commits into from
Jul 26, 2022
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ services:
context: ./
dockerfile: ./src/recommendationservice/Dockerfile
ports:
- "${RECOMMENDATION_SERVICE_PORT}"
- "${RECOMMENDATION_SERVICE_PORT}:${RECOMMENDATION_SERVICE_PORT}"
depends_on:
- productcatalogservice
- otelcol
Expand Down
29 changes: 0 additions & 29 deletions src/recommendationservice/README.md
Original file line number Diff line number Diff line change
@@ -1,30 +1 @@
# Read Me

1. Expose the service's port to the localhost by modifying `/compose.yml`

```diff
- - "${RECOMMENDATION_SERVICE_PORT}"
+ - "${RECOMMENDATION_SERVICE_PORT}:${RECOMMENDATION_SERVICE_PORT}"
```

1. To run the `./client.py` you must compile `/pb/demo.proto` into python code

```shell
python -m grpc_tools.protoc -I./pb/ --python_out=./src/recommendationservice/ --grpc_python_out=./src/recommendationservice/ ./pb/demo.proto
python ./src/recommendationservice/client.py
```

1. You should see output similar to the following

```json
{
"asctime": "2022-06-02 13:42:44,793",
"levelname": "INFO",
"name": "recommendationservice-server",
"filename": "client.py",
"lineno": 35,
"otelTraceID": "00000000000000000000000000000000",
"otelSpanID": "0000000000000000",
"message": "product_ids: \"6E92ZMYYFZ\"\nproduct_ids: \"OLJCESPC7Z\"\nproduct_ids: \"LS4PSXUNUM\"\nproduct_ids: \"2ZYFJ3GM2N\"\nproduct_ids: \"1YMWWN1N4O\"\n"
}
```
44 changes: 0 additions & 44 deletions src/recommendationservice/client.py

This file was deleted.

41 changes: 16 additions & 25 deletions src/recommendationservice/recommendation_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,30 +14,25 @@
# See the License for the specific language governing permissions and
# limitations under the License.

# Python
import os
import random
import time
from concurrent import futures

# Pip
import grpc

import demo_pb2
import demo_pb2_grpc
from grpc_health.v1 import health_pb2
from grpc_health.v1 import health_pb2_grpc

from opentelemetry import trace
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.trace.export import (BatchSpanProcessor)
from opentelemetry.exporter.otlp.proto.grpc.trace_exporter import OTLPSpanExporter

# Local
import demo_pb2
import demo_pb2_grpc
from grpc_health.v1 import health_pb2
from grpc_health.v1 import health_pb2_grpc
from logger import getJSONLogger
logger = getJSONLogger('recommendationservice-server')

tracer_provider = TracerProvider()
trace.set_tracer_provider(tracer_provider)
tracer_provider.add_span_processor(BatchSpanProcessor(OTLPSpanExporter()))
tracer = trace.get_tracer("recommendationservice")


class RecommendationService(demo_pb2_grpc.RecommendationServiceServicer):
Expand Down Expand Up @@ -86,33 +81,29 @@ def must_map_env(key: str):
raise Exception(f'{key} environment variable must be set')
return value


if __name__ == "__main__":
logger.info("initializing recommendationservice")
logger = getJSONLogger('recommendationservice-server')
tracer_provider = TracerProvider()
trace.set_tracer_provider(tracer_provider)
tracer_provider.add_span_processor(BatchSpanProcessor(OTLPSpanExporter()))
tracer = trace.get_tracer("recommendationservice")

port = must_map_env('RECOMMENDATION_SERVICE_PORT')
catalog_addr = must_map_env('PRODUCT_CATALOG_SERVICE_ADDR')

logger.info("product catalog address: " + catalog_addr)
channel = grpc.insecure_channel(catalog_addr)
product_catalog_stub = demo_pb2_grpc.ProductCatalogServiceStub(channel)

# create gRPC server
server = grpc.server(futures.ThreadPoolExecutor(max_workers=10),)
server = grpc.server(futures.ThreadPoolExecutor(max_workers=10))

# add class to gRPC server
service = RecommendationService()
demo_pb2_grpc.add_RecommendationServiceServicer_to_server(service, server)
health_pb2_grpc.add_HealthServicer_to_server(service, server)

# start server
logger.info("listening on port: " + port)
server.add_insecure_port('[::]:'+port)
logger.info("RecommendationService listening on port: " + port)
server.add_insecure_port('[::]:' + port)
server.start()

# keep alive
try:
while True:
time.sleep(10000)
except KeyboardInterrupt:
server.stop(0)
server.wait_for_termination()
4 changes: 4 additions & 0 deletions test/data.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,9 @@
"creditCardExpirationYear": 2039,
"creditCardExpirationMonth": 1
}
},
"recommend": {
"userId": "1234",
"productIds": [ "OLJCESPC7Z", "66VCHSJNUP", "1YMWWN1N4O", "L9ECAV7KIM", "2ZYFJ3GM2N" ]
}
}
17 changes: 17 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,11 @@ const data = require('./data.json')
// Functions
const deepCopy = obj => JSON.parse(JSON.stringify(obj))

const arrayIntersection = (a, b) => a.filter(x => b.indexOf(x) !== -1)

// Main
let charge = null
let recommend = null

test.before(() => {
dotenv.config({ path: '../.env' })
Expand All @@ -23,6 +26,9 @@ test.before(() => {

const paymentClient = new hipstershop.PaymentService(`0.0.0.0:${process.env.PAYMENT_SERVICE_PORT}`, grpc.credentials.createInsecure())
charge = promisify(paymentClient.charge).bind(paymentClient)

const recommendationClient = new hipstershop.RecommendationService(`0.0.0.0:${process.env.RECOMMENDATION_SERVICE_PORT}`, grpc.credentials.createInsecure())
recommend = promisify(recommendationClient.listRecommendations).bind(recommendationClient)
})

// --------------- Payment Service ---------------
Expand Down Expand Up @@ -61,3 +67,14 @@ test('payment: expired credit card', t => {
t.is(err.details, 'The credit card (ending 0454) expired on 1/2021.')
})
})

// --------------- Recommendation Service ---------------

test('recommendation: list products', t => {
const request = deepCopy(data.recommend)

return recommend(request).then(res => {
t.is(res.productIds.length, 4)
t.is(arrayIntersection(res.productIds, request.productIds).length, 0)
})
})