def test_create_channel_default(self): with spy_func("grpc.insecure_channel") as m1: with spy_func("grpc.intercept_channel") as m2: create_channel(self.target) m1.assert_called_once_with(self.target, [ ("grpc.max_send_message_length", grpc_max_msg_size), ("grpc.max_receive_message_length", grpc_max_msg_size), ]) m2.assert_called_once_with(mock.ANY)
def test_create_channel_with_interceptors(self): i1 = LogUnaryClientInterceptor(lambda: None) i2 = LogStreamClientInterceptor(lambda: None) with spy_func("grpc.insecure_channel") as m1: with spy_func("grpc.intercept_channel") as m2: create_channel(self.target, interceptors=[i1, i2]) m1.assert_called_once_with(self.target, [ ("grpc.max_send_message_length", grpc_max_msg_size), ("grpc.max_receive_message_length", grpc_max_msg_size), ]) m2.assert_called_once_with(mock.ANY, i1, i2)
def NotifyReviewEvent(self, request, context): print("got review request {}".format(request)) comments = [] # client connection to DataServe with create_channel(data_srv_addr) as channel: stub = pb.DataStub(channel) changes = stub.GetChanges( pb.ChangesRequest(head=request.commit_revision.head, base=request.commit_revision.base, want_contents=False, want_uast=True, exclude_vendored=True)) for change in changes: if not change.HasField("head"): continue print("analyzing '{}' in {}".format(change.head.path, change.head.language)) fns = list(filter_uast(change.head.uast, "//*[@roleFunction]")) text = "language: {}, functions: {}".format( change.head.language, len(fns)) comments.append( pb.Comment(file=change.head.path, line=0, text=text)) return pb.EventResponse(analyzer_version=version, comments=comments)
def _get_channel(self) -> grpc.Channel: channel = getattr(self._data_request_local, "channel", None) if channel is None: self._data_request_local.channel = channel = create_channel(self._data_request_address) self._data_request_channels.append(channel) self._log.info("Opened %s", channel) return channel
def test_push(self): with create_channel(self._target) as channel: stub = pb.AnalyzerStub(channel) resp = stub.NotifyPushEvent(event_pb2.PushEvent()) self.assertEqual(len(resp.comments), 1) self.assertEqual(resp.comments[0].text, 'push')
def notify_review_event(self, request, context): logger.debug("got review request %s", request) comments = [] # client connection to DataServe with create_channel(data_srv_addr, interceptors=[ LogUnaryClientInterceptor(log_fn), LogStreamClientInterceptor(log_fn), ]) as channel: stub = DataStub(channel) changes = stub.get_changes( context, pb.ChangesRequest(head=request.commit_revision.head, base=request.commit_revision.base, want_contents=False, want_uast=True, exclude_vendored=True, include_languages=langs)) for change in changes: if not change.HasField("head"): continue logger.debug("analyzing '%s' in %s", change.head.path, change.head.language) try: check_results = run_checks( list_checks(change.head.language.lower()), change.head.language.lower(), change.head.uast) except Exception as e: logger.exception( "Error during analyzing file '%s' in commit '%s': %s", change.head.path, request.commit_revision.head.hash, e) continue for check in check_results: for res in check_results[check]: comments.append( pb.Comment(file=change.head.path, line=(res.get("pos", {}) or {}).get("line", 0), text="{}: {}".format(check, res["msg"]))) logger.info("%d comments produced", len(comments)) return pb.EventResponse(analyzer_version=version, comments=comments)
def test_interceptors_called(self): with create_channel(self._target, interceptors=[ LogUnaryClientInterceptor(self._tracker.unary), LogStreamClientInterceptor( self._tracker.stream), ]) as channel: stub = DataStub(channel) stub.get_changes(None, pb.ChangesRequest()) self.assertEqual(self._tracker.counter, {"unary": 0, "stream": 2}) first_streaming = self._tracker.logs[0] first_log_fields = first_streaming[0] self.assertEqual( first_log_fields, { "system": "grpc", "span.kind": "client", "grpc.service": "pb.Data", "grpc.method": "GetChanges", }) self.assertEqual(first_streaming[1], "gRPC streaming client call started") second_streaming = self._tracker.logs[1] second_log_fields = second_streaming[0] self.assertEqual( set(second_log_fields.keys()), { "system", "span.kind", "grpc.service", "grpc.method", "grpc.code", "grpc.start_time", "duration" }) second_log_fields.pop("grpc.start_time") second_log_fields.pop("duration") self.assertEqual( second_log_fields, { "system": "grpc", "span.kind": "client", "grpc.service": "pb.Data", "grpc.method": "GetChanges", "grpc.code": "OK" }) self.assertEqual(second_streaming[1], "gRPC streaming client call finished")
def notify_review_event(self, request, context): print("got review request {}".format(request)) comments = [] # client connection to DataServe with create_channel(data_srv_addr, interceptors=[ LogUnaryClientInterceptor(log_fn), LogStreamClientInterceptor(log_fn), ]) as channel: stub = DataStub(channel) # Add some log fields that will be available to the data server # using `context.add_log_fields`. context.add_log_fields({ "some-string-key": "some-value", "some-int-key": 1, }) changes = stub.get_changes( context, pb.ChangesRequest(head=request.commit_revision.head, base=request.commit_revision.base, want_contents=False, want_uast=True, exclude_vendored=True)) for change in changes: if not change.HasField("head"): continue print("analyzing '{}' in {}".format(change.head.path, change.head.language)) fns = list(filter_uast(change.head.uast, "//*[@roleFunction]")) text = "language: {}, functions: {}".format( change.head.language, len(fns)) comments.append( pb.Comment(file=change.head.path, line=0, text=text)) return pb.EventResponse(analyzer_version=version, comments=comments)
def test_interceptors_called(self): with create_channel(self._target) as channel: stub = pb.AnalyzerStub(channel) stub.NotifyReviewEvent(event_pb2.ReviewEvent()) self.assertEqual(self._tracker.counter, {"unary": 2, "stream": 0}) first_unary = self._tracker.logs[0] first_log_fields = first_unary[0] self.assertEqual( first_log_fields, { "system": "grpc", "span.kind": "server", "grpc.service": "pb.Analyzer", "grpc.method": "NotifyReviewEvent", }) self.assertEqual(first_unary[1], "gRPC unary server call started") second_unary = self._tracker.logs[1] second_log_fields = second_unary[0] self.assertEqual( set(second_log_fields.keys()), { "system", "span.kind", "grpc.service", "grpc.method", "grpc.code", "grpc.start_time", "duration" }) second_log_fields.pop("grpc.start_time") second_log_fields.pop("duration") self.assertEqual( second_log_fields, { "system": "grpc", "span.kind": "server", "grpc.service": "pb.Analyzer", "grpc.method": "NotifyReviewEvent", "grpc.code": "OK" }) self.assertEqual(second_unary[1], "gRPC unary server call finished")