github.com/emcfarlane/larking@v0.0.0-20220605172417-1704b45ee6c3/testdata/grpc.star (about)

     1  # Tests of Starlark 'grpc' extension.
     2  
     3  load("encoding/proto.star", "proto")
     4  
     5  # TODO: show dialing to add a new stream.
     6  #grpc.dial("//")
     7  
     8  def test_message(t):
     9      s = mux.service("larking.testpb.Messaging")
    10  
    11      print(s)
    12      m = s.GetMessageOne({
    13          "name": "starlark",
    14      })
    15  
    16      t.eq(m.message_id, "starlark")
    17      t.eq(m.text, "hello")
    18      t.eq(m.user_id, "user")
    19  
    20  # TODO: handle the reflection stream.
    21  #pb = proto.new("larking.testpb")
    22  #x = pb.Message(message_id = "starlark", text = "hello", user_id = "user")
    23  
    24  pb = proto.file("testpb/test.proto")
    25  bodypb = proto.file("google/api/httpbody.proto")
    26  
    27  # Register a starlark service.
    28  def test_register_service(t):
    29      t.skip()  # TODO: ensure service is called.
    30      s = mux.service("larking.testpb.Files")
    31  
    32      def upload_download(req):
    33          print("TODO: capture me")
    34          return bodypb.HttpBody(
    35              content_type = "text/plain",
    36              data = str(req.file.data) + ", starlark!",
    37          )
    38  
    39      def large_upload_download(stream):
    40          req = stream.recv()
    41          print("upload request", req)
    42  
    43          msg = pb.Mesasge(
    44              message_id = "123",
    45              text = "hello, starlark!",
    46          )
    47          data = proto.marshal_json(msg)
    48          n = len(data) / 3
    49          chunks = [data[i:i + n] for i in range(0, len(data), n)]
    50  
    51          for chunk in chunks:
    52              stream.send(bodypb.HttpBody(
    53                  content_type = "application/json",
    54                  data = chunk,
    55              ))
    56  
    57      # register_service
    58      mux.register_service(
    59          "larking.testpb.Files",
    60          UploadDownload = upload_download,
    61          LargeUploadDownload = large_upload_download,
    62      )
    63  
    64      rsp = s.UploadDownload(
    65          filename = "message.txt",
    66          file = bodypb.HttpBody(
    67              content_type = "text/plain",
    68              data = "hello",
    69          ),
    70      )
    71      print(rsp)
    72      t.eq(rsp.content_type, "text/plain")
    73      t.eq(rsp.data, "hello, starlark!")
    74  
    75      # TODO: streaming...
    76      #stream = s.LargeUploadDownload()
    77      #stream.send()
    78      #stream.recv()