Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
Clear All
new posts

    Best practice: Creating a mocked response serverside

    Hi Isomorphic,

    how do you create a mocked DSReponse serverside?
    There is no constructor taking a DataSource name as String, only ones taking DataSource objects.

    I have this code, which is missing a DataSourceManager.free(). Can I put it just before the return? Or will this somehow affect the DSResponse as well?
    Code:
            DSResponse fakeRemoveResponse = new DSResponse(DataSourceManager.get(DatasourceEnum.T_CHAT.getValue()));
            fakeRemoveResponse.setSuccess();
            fakeRemoveResponse.setAffectedRows(1);
            fakeRemoveResponse.setOperationType(DataSource.OP_REMOVE);
            Map<String, Object> returnValueMap = new LinkedHashMap<String, Object>();
            returnValueMap.put(DatasourceFieldEnum.T_CHAT__ID.getValue(), chatId);
            fakeRemoveResponse.setData(returnValueMap);
    
            // Faked response of self-removal
            return new DSResponse().setSuccess().addRelatedUpdate(fakeRemoveResponse);
    This hint from RPCManager.getDataSource() does not apply, as I don't have such constructor on DSResponse, that is available on DSRequest:
    Also, if you are trying to access some arbitrary DataSource purely because you need to run a DSRequest on it (another very common use case), consider just creating the DSRequest instead, using one of the constructors that accepts a DataSource name.
    Thank you & Best regards
    Blama

    #2
    Please try explaining again: when you say "mock" do you mean mocking as in the testing technique? If so, what specific problem is the framework creating in terms of mocking for testing purposes?

    If you meant some other kind of mocking, please explain what you had in mind.

    Comment


      #3
      Hi Isomorphic,

      the use case is following: After an ADD I know the result of an potential FETCH in advance - no result (e.g. all not added products will miss the entry I just added).
      In order to remove that entry client side I need to fake a REMOVE DSResponse on the server an send it with addRelatedUpdate().
      I do not need any real DB interaction as I know the result beforehand, so I don't want a DSRequest.execute(), but need to create a DSResponse anyway.

      This is also, what it happening in the code excerpt in #1.

      Best regards
      Blama

      Comment


        #4
        OK, now we understand the use case, but still don't understand what you are concerned about. If you return a DSResponse like you show, that's fine, and the usual rules for DataSource allocation and freeing apply - it doesn't matter whether a DSResponse happens to have been executed, the rules are the same.

        Comment


          #5
          Hi Isomorphic,

          my problem/question is the following:
          With the code from #1, I'm calling myself DataSourceManager.get(), but never DataSourceManager.free(). Is this OK? The docs suggest otherwise IMHO:
          Returns a DataSource object from the pool, creating a new instance if necessary. Note that you must be careful to return the DataSource object to the pool using the DataSourceManager.free(com.isomorphic.datasource.DataSource) method, or you will leak objects.
          If not, is this code OK?
          Code:
          DataSource ds = DataSourceManager.get(DatasourceEnum.T_CHAT.getValue());
          DSResponse fakeRemoveResponse = new DSResponse(ds);
          fakeRemoveResponse.setSuccess();
          fakeRemoveResponse.setAffectedRows(1);
          fakeRemoveResponse.setOperationType(DataSource.OP_REMOVE);
          Map<String, Object> returnValueMap = new LinkedHashMap<String, Object>();
          returnValueMap.put(DatasourceFieldEnum.T_CHAT__ID.getValue(), chatId);
          fakeRemoveResponse.setData(returnValueMap);
          
          [B]// Is this too early because the fakeRemoveResponse-objects still holds a reference to ds?
          // If it is too early, how to solve this?[/B]
          DataSourceManager.free(ds);
          
          // Faked response of self-removal
          return new DSResponse().setSuccess().addRelatedUpdate(fakeRemoveResponse);
          Thank you & Best regards
          Blama

          Comment


            #6
            Yes, the documentation is accurate and you must call free if you retrieve a DataSource from the DataSourceManager.

            It would be invalid to free the DataSource before the DSResponse has used it, but you don't need to worry about timing since RPCManager.getDataSource() can be used to get automatic freeing.

            Comment


              #7
              Hi Isomorphic,

              to summarize:
              • Code in #1 wrong because of missing DataSourceManager.free(ds)
              • Code in #5 wrong because of too early DataSourceManager.free(ds)
              • Your suggestion is to use RPCManager.getDataSource() instead, if one is available, like you write also here in the docs
              The last open question is how would I need to change my code in #5 if I don't have an RPCManager because of standalone usage?

              Thank you & Best regards
              Blama

              Comment


                #8
                For standalone usage, call free() as indicated in the docs.

                Comment

                Working...
                X