initial commit
This commit is contained in:
7
exercises/07_threads/05_channels/Cargo.toml
Normal file
7
exercises/07_threads/05_channels/Cargo.toml
Normal file
@@ -0,0 +1,7 @@
|
||||
[package]
|
||||
name = "channels"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
[dependencies]
|
||||
ticket_fields = { path = "../../../helpers/ticket_fields" }
|
||||
23
exercises/07_threads/05_channels/src/data.rs
Normal file
23
exercises/07_threads/05_channels/src/data.rs
Normal file
@@ -0,0 +1,23 @@
|
||||
use crate::store::TicketId;
|
||||
use ticket_fields::{TicketDescription, TicketTitle};
|
||||
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
pub struct Ticket {
|
||||
pub id: TicketId,
|
||||
pub title: TicketTitle,
|
||||
pub description: TicketDescription,
|
||||
pub status: Status,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, Eq)]
|
||||
pub struct TicketDraft {
|
||||
pub title: TicketTitle,
|
||||
pub description: TicketDescription,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Copy, PartialEq, Eq)]
|
||||
pub enum Status {
|
||||
ToDo,
|
||||
InProgress,
|
||||
Done,
|
||||
}
|
||||
23
exercises/07_threads/05_channels/src/lib.rs
Normal file
23
exercises/07_threads/05_channels/src/lib.rs
Normal file
@@ -0,0 +1,23 @@
|
||||
use std::sync::mpsc::{Receiver, Sender};
|
||||
|
||||
pub mod data;
|
||||
pub mod store;
|
||||
|
||||
pub enum Command {
|
||||
Insert(todo!()),
|
||||
}
|
||||
|
||||
// Start the system by spawning the server thread.
|
||||
// It returns a `Sender` instance which can then be used
|
||||
// by one or more clients to interact with the server.
|
||||
pub fn launch() -> Sender<Command> {
|
||||
let (sender, receiver) = std::sync::mpsc::channel();
|
||||
std::thread::spawn(move || server(receiver));
|
||||
sender
|
||||
}
|
||||
|
||||
// TODO: The server task should **never** stop.
|
||||
// Enter a loop: wait for a command to show up in
|
||||
// the channel, then execute it, then start waiting
|
||||
// for the next command.
|
||||
pub fn server(receiver: Receiver<Command>) {}
|
||||
33
exercises/07_threads/05_channels/src/store.rs
Normal file
33
exercises/07_threads/05_channels/src/store.rs
Normal file
@@ -0,0 +1,33 @@
|
||||
use crate::data::{Status, Ticket, TicketDraft};
|
||||
use std::collections::BTreeMap;
|
||||
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord)]
|
||||
pub struct TicketId(u64);
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct TicketStore {
|
||||
tickets: BTreeMap<TicketId, Ticket>,
|
||||
counter: u64,
|
||||
}
|
||||
|
||||
impl TicketStore {
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
tickets: BTreeMap::new(),
|
||||
counter: 0,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn add_ticket(&mut self, ticket: TicketDraft) -> TicketId {
|
||||
let id = TicketId(self.counter);
|
||||
self.counter += 1;
|
||||
let ticket = Ticket {
|
||||
id,
|
||||
title: ticket.title,
|
||||
description: ticket.description,
|
||||
status: Status::ToDo,
|
||||
};
|
||||
self.tickets.insert(id, ticket);
|
||||
id
|
||||
}
|
||||
}
|
||||
32
exercises/07_threads/05_channels/tests/insert.rs
Normal file
32
exercises/07_threads/05_channels/tests/insert.rs
Normal file
@@ -0,0 +1,32 @@
|
||||
// TODO: Set `move_forward` to `true` in `ready` when you think you're done with this exercise.
|
||||
// Feel free to call an instructor to verify your solution!
|
||||
use channels::data::TicketDraft;
|
||||
use channels::{launch, Command};
|
||||
use std::time::Duration;
|
||||
use ticket_fields::test_helpers::{ticket_description, ticket_title};
|
||||
|
||||
#[test]
|
||||
fn a_thread_is_spawned() {
|
||||
let sender = launch();
|
||||
std::thread::sleep(Duration::from_millis(200));
|
||||
|
||||
sender
|
||||
.send(Command::Insert(TicketDraft {
|
||||
title: ticket_title(),
|
||||
description: ticket_description(),
|
||||
}))
|
||||
// If the thread is no longer running, this will panic
|
||||
// because the channel will be closed.
|
||||
.expect("Did you actually spawn a thread? The channel is closed!");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn ready() {
|
||||
// There's very little that we can check automatically in this exercise,
|
||||
// since our server doesn't expose any **read** actions.
|
||||
// We have no way to know if the inserts are actually happening and if they
|
||||
// are happening correctly.
|
||||
let move_forward = false;
|
||||
|
||||
assert!(move_forward);
|
||||
}
|
||||
Reference in New Issue
Block a user