tornado_json unit testing async methods












0















I'm trying to set up a unit test for a tornado_json web app.
I'm trying to test a post handler, but I'm failing miserably as the fetch method seems to return an _asyncio.Future object, which never seems to complete/have a result set. I've tried to post a summary of the code, at the moment I'm just returning ['test'] item.
I've looked at https://github.com/tornadoweb/tornado/issues/1154, as well as the tornado documentation. It sounds like I need to self.stop, or self.wait() to complete the task, but I haven't worked out how to get this to work, or if that is the solution.
Any help would be greatly appreciated.




@schema.validate(
input_schema={
"type": "object",
"properties": {
"path": {"type": "string"}
},
"required": ["path"]
},
output_schema={
"type": "array",
"items": {
"properties": {"type": "string"}
}
}
)
@coroutine
def post(self):
attributes = dict(self.body)
path = attributes["path"]
response = ["test"]
return response




@gen_test
def test_POST_method(self):

body = json.dumps({'path': 'bin'})
self.http_client.fetch(self.get_url('/api/listmyfiles'),
method="POST",
body=body
)
response = self.wait()
print(response.result()))



The error I get is:
asyncio.base_futures.InvalidStateError: Result is not set.










share|improve this question





























    0















    I'm trying to set up a unit test for a tornado_json web app.
    I'm trying to test a post handler, but I'm failing miserably as the fetch method seems to return an _asyncio.Future object, which never seems to complete/have a result set. I've tried to post a summary of the code, at the moment I'm just returning ['test'] item.
    I've looked at https://github.com/tornadoweb/tornado/issues/1154, as well as the tornado documentation. It sounds like I need to self.stop, or self.wait() to complete the task, but I haven't worked out how to get this to work, or if that is the solution.
    Any help would be greatly appreciated.




    @schema.validate(
    input_schema={
    "type": "object",
    "properties": {
    "path": {"type": "string"}
    },
    "required": ["path"]
    },
    output_schema={
    "type": "array",
    "items": {
    "properties": {"type": "string"}
    }
    }
    )
    @coroutine
    def post(self):
    attributes = dict(self.body)
    path = attributes["path"]
    response = ["test"]
    return response




    @gen_test
    def test_POST_method(self):

    body = json.dumps({'path': 'bin'})
    self.http_client.fetch(self.get_url('/api/listmyfiles'),
    method="POST",
    body=body
    )
    response = self.wait()
    print(response.result()))



    The error I get is:
    asyncio.base_futures.InvalidStateError: Result is not set.










    share|improve this question



























      0












      0








      0








      I'm trying to set up a unit test for a tornado_json web app.
      I'm trying to test a post handler, but I'm failing miserably as the fetch method seems to return an _asyncio.Future object, which never seems to complete/have a result set. I've tried to post a summary of the code, at the moment I'm just returning ['test'] item.
      I've looked at https://github.com/tornadoweb/tornado/issues/1154, as well as the tornado documentation. It sounds like I need to self.stop, or self.wait() to complete the task, but I haven't worked out how to get this to work, or if that is the solution.
      Any help would be greatly appreciated.




      @schema.validate(
      input_schema={
      "type": "object",
      "properties": {
      "path": {"type": "string"}
      },
      "required": ["path"]
      },
      output_schema={
      "type": "array",
      "items": {
      "properties": {"type": "string"}
      }
      }
      )
      @coroutine
      def post(self):
      attributes = dict(self.body)
      path = attributes["path"]
      response = ["test"]
      return response




      @gen_test
      def test_POST_method(self):

      body = json.dumps({'path': 'bin'})
      self.http_client.fetch(self.get_url('/api/listmyfiles'),
      method="POST",
      body=body
      )
      response = self.wait()
      print(response.result()))



      The error I get is:
      asyncio.base_futures.InvalidStateError: Result is not set.










      share|improve this question
















      I'm trying to set up a unit test for a tornado_json web app.
      I'm trying to test a post handler, but I'm failing miserably as the fetch method seems to return an _asyncio.Future object, which never seems to complete/have a result set. I've tried to post a summary of the code, at the moment I'm just returning ['test'] item.
      I've looked at https://github.com/tornadoweb/tornado/issues/1154, as well as the tornado documentation. It sounds like I need to self.stop, or self.wait() to complete the task, but I haven't worked out how to get this to work, or if that is the solution.
      Any help would be greatly appreciated.




      @schema.validate(
      input_schema={
      "type": "object",
      "properties": {
      "path": {"type": "string"}
      },
      "required": ["path"]
      },
      output_schema={
      "type": "array",
      "items": {
      "properties": {"type": "string"}
      }
      }
      )
      @coroutine
      def post(self):
      attributes = dict(self.body)
      path = attributes["path"]
      response = ["test"]
      return response




      @gen_test
      def test_POST_method(self):

      body = json.dumps({'path': 'bin'})
      self.http_client.fetch(self.get_url('/api/listmyfiles'),
      method="POST",
      body=body
      )
      response = self.wait()
      print(response.result()))



      The error I get is:
      asyncio.base_futures.InvalidStateError: Result is not set.







      python-3.x tornado pytest pytest-asyncio






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 14 '18 at 12:02







      Conor

















      asked Nov 14 '18 at 11:52









      ConorConor

      1199




      1199
























          1 Answer
          1






          active

          oldest

          votes


















          1














          AsyncHTTPTestCase has a few different modes of operation that can't be mixed.





          1. @gen_test: Used with await self.http_client.fetch(self.get_url(...)):



            @gen_test
            async def test_post_method(self):
            response = await self.http_client.fetch(self.get_url(...))


          2. self.stop/self.wait is an older interface that is mostly (but not completely) deprecated. AsyncHTTPClient will not be (easily) compatible with this interface in Tornado 6.0, so I won't show an example here.



          3. self.fetch is a shorthand method that combines the calls to http_client.fetch and self.get_url, and uses stop/wait under the covers (so it is not compatible with @gen_test):



            def test_post_method(self):
            response = self.fetch('/api/listmyfiles')



          If the only asynchronous thing you're doing is HTTP fetches, you can use self.fetch. If you need to do anything else asynchronous, use gen_test and avoid the stop/wait/self.fetch methods.






          share|improve this answer























            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53299630%2ftornado-json-unit-testing-async-methods%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            1














            AsyncHTTPTestCase has a few different modes of operation that can't be mixed.





            1. @gen_test: Used with await self.http_client.fetch(self.get_url(...)):



              @gen_test
              async def test_post_method(self):
              response = await self.http_client.fetch(self.get_url(...))


            2. self.stop/self.wait is an older interface that is mostly (but not completely) deprecated. AsyncHTTPClient will not be (easily) compatible with this interface in Tornado 6.0, so I won't show an example here.



            3. self.fetch is a shorthand method that combines the calls to http_client.fetch and self.get_url, and uses stop/wait under the covers (so it is not compatible with @gen_test):



              def test_post_method(self):
              response = self.fetch('/api/listmyfiles')



            If the only asynchronous thing you're doing is HTTP fetches, you can use self.fetch. If you need to do anything else asynchronous, use gen_test and avoid the stop/wait/self.fetch methods.






            share|improve this answer




























              1














              AsyncHTTPTestCase has a few different modes of operation that can't be mixed.





              1. @gen_test: Used with await self.http_client.fetch(self.get_url(...)):



                @gen_test
                async def test_post_method(self):
                response = await self.http_client.fetch(self.get_url(...))


              2. self.stop/self.wait is an older interface that is mostly (but not completely) deprecated. AsyncHTTPClient will not be (easily) compatible with this interface in Tornado 6.0, so I won't show an example here.



              3. self.fetch is a shorthand method that combines the calls to http_client.fetch and self.get_url, and uses stop/wait under the covers (so it is not compatible with @gen_test):



                def test_post_method(self):
                response = self.fetch('/api/listmyfiles')



              If the only asynchronous thing you're doing is HTTP fetches, you can use self.fetch. If you need to do anything else asynchronous, use gen_test and avoid the stop/wait/self.fetch methods.






              share|improve this answer


























                1












                1








                1







                AsyncHTTPTestCase has a few different modes of operation that can't be mixed.





                1. @gen_test: Used with await self.http_client.fetch(self.get_url(...)):



                  @gen_test
                  async def test_post_method(self):
                  response = await self.http_client.fetch(self.get_url(...))


                2. self.stop/self.wait is an older interface that is mostly (but not completely) deprecated. AsyncHTTPClient will not be (easily) compatible with this interface in Tornado 6.0, so I won't show an example here.



                3. self.fetch is a shorthand method that combines the calls to http_client.fetch and self.get_url, and uses stop/wait under the covers (so it is not compatible with @gen_test):



                  def test_post_method(self):
                  response = self.fetch('/api/listmyfiles')



                If the only asynchronous thing you're doing is HTTP fetches, you can use self.fetch. If you need to do anything else asynchronous, use gen_test and avoid the stop/wait/self.fetch methods.






                share|improve this answer













                AsyncHTTPTestCase has a few different modes of operation that can't be mixed.





                1. @gen_test: Used with await self.http_client.fetch(self.get_url(...)):



                  @gen_test
                  async def test_post_method(self):
                  response = await self.http_client.fetch(self.get_url(...))


                2. self.stop/self.wait is an older interface that is mostly (but not completely) deprecated. AsyncHTTPClient will not be (easily) compatible with this interface in Tornado 6.0, so I won't show an example here.



                3. self.fetch is a shorthand method that combines the calls to http_client.fetch and self.get_url, and uses stop/wait under the covers (so it is not compatible with @gen_test):



                  def test_post_method(self):
                  response = self.fetch('/api/listmyfiles')



                If the only asynchronous thing you're doing is HTTP fetches, you can use self.fetch. If you need to do anything else asynchronous, use gen_test and avoid the stop/wait/self.fetch methods.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 14 '18 at 14:17









                Ben DarnellBen Darnell

                17.6k22036




                17.6k22036






























                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53299630%2ftornado-json-unit-testing-async-methods%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    Xamarin.iOS Cant Deploy on Iphone

                    Glorious Revolution

                    Dulmage-Mendelsohn matrix decomposition in Python