github.com/muhammedhassanm/blockchain@v0.0.0-20200120143007-697261defd4d/sawtooth-core-master/sdk/rust/src/consensus/driver.rs (about)

     1  /*
     2   * Copyright 2018 Intel Corporation
     3   *
     4   * Licensed under the Apache License, Version 2.0 (the "License");
     5   * you may not use this file except in compliance with the License.
     6   * You may obtain a copy of the License at
     7   *
     8   *     http://www.apache.org/licenses/LICENSE-2.0
     9   *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   * ------------------------------------------------------------------------------
    16   */
    17  
    18  use consensus::engine::{Engine, Error};
    19  
    20  pub trait Driver {
    21      fn new(engine: Box<Engine>) -> Self;
    22      fn start(&self, endpoint: &str) -> Result<(), Error>;
    23      fn stop(&self);
    24  }
    25  
    26  #[cfg(test)]
    27  pub mod tests {
    28      use super::*;
    29  
    30      use std::sync::mpsc::channel;
    31  
    32      use consensus::engine::tests::MockEngine;
    33      use consensus::service::tests::MockService;
    34  
    35      pub struct MockDriver {
    36          engine: Box<Engine>,
    37      }
    38  
    39      impl Driver for MockDriver {
    40          fn new(engine: Box<Engine>) -> Self {
    41              MockDriver { engine }
    42          }
    43  
    44          fn start(&self, _endpoint: &str) -> Result<(), Error> {
    45              let service = Box::new(MockService {});
    46              let (_sender, receiver) = channel();
    47              self.engine
    48                  .start(receiver, service, Default::default());
    49              Ok(())
    50          }
    51  
    52          fn stop(&self) {}
    53      }
    54  
    55      #[test]
    56      fn test_harness() {
    57          // Just test that we can create a driver
    58          let engine = Box::new(MockEngine::new());
    59          MockDriver::new(engine);
    60      }
    61  }