Giter Site home page Giter Site logo

localrest's Introduction

LocalRest

DUB Package codecov

A library to allow integration testing of vibe.web.rest based code as unittests.

Example

The main source file is extensively documented, and includes many unittests.

The straightforward approach is to do:

    static interface API
    {
        @safe: // Vibe.d requirement, but LocalRest works with `@system`
        public @property ulong pubkey ();
        public string getValue (ulong idx);
        public ubyte[32] getQuorumSet ();
        public string recv (string data);
    }

    static class MockAPI : API
    {
        @safe:
        public override @property ulong pubkey ()
        { return 42; }
        public override string getValue (ulong idx)
        { assert(0); }
        public override ubyte[32] getQuorumSet ()
        { assert(0); }
        public override string recv (string data)
        { assert(0); }
    }

    // This will start a new thread, create a `MockAPI` object in it,
    // and return a handle that allows communicating with it.
    scope API test = RemoteAPI!API.spawn!MockAPI();
    // Note that the handle will be of type `RemoteAPI!API`, which implements `API`
    // This is so one can have an array of `API`, but different underlying implementations.

    // This will send a message to the remote thread, which will ultimately call `MockAPI.pubkey`
    // Any parameter and return value is serialized to a type that is safe to pass accross thread.
    // By default, Vibe.d's JSON serializer is used, but it can be replaced (see `geod24.Serialization`).
    assert(test.pubkey() == 42);

    // Remote nodes can be controlled, to extend testing parameters:
    // For example, they can be made to be unresponsive for a set duration,
    // or to ignore calls to certain methods.
    test.ctrl.shutdown();

For this to work, one need to have Vibe.d already fetched. For a simple test, dub fetch vibe-d will suffice. For project depending on this, make sure your dub.json includes the following in dependencies:

{
    "vibe-d:data": "~>0.8"
}

localrest's People

Contributors

andrejmitrovic avatar geod24 avatar michaelkim20 avatar omerfirmak avatar trusthenry avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.