Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

exercises/07_threads/07_ack Issue: Type Mismatch in Test - Returning Option Instead of Ticket and Unwrap Usage #242

Open
RioCasanova opened this issue Jan 1, 2025 · 0 comments

Comments

@RioCasanova
Copy link

Issue: Type Mismatch in Test - Returning Option Instead of Ticket and Unwrap Usage.

feel free to check my fork for this exercise: https://github.com/RioCasanova/100-exercises-to-learn-rust

Description:

The current implementation of the server code returns an Option<Ticket> from store.get(id) and sends it using the response_sender. However, the test expects the return type of the response_receiver to be a Ticket, not an Option<Ticket>. This mismatch causes a type error during compilation. Additionally, the test is calling .unwrap() on the Ticket, which doesn't align with the expected type handling.

The test is failing with the following error:

error[E0599]: no method named `unwrap` found for struct `Ticket` in the current scope
  --> exercises/07_threads/07_ack/tests/insert.rs:40:10
   |
   37 |       let ticket: Ticket = response_receiver
   |  __________________________-
   38 | |         .recv()
   | |          ------ method `unwrap` is available on `Result<Ticket, RecvError>`
   |  39 | |         .expect("No response received!")
   |  40 | |         .unwrap();
   |  |         -^^^^^^ method not found in `Ticket`
   |  |_________|

Problem:

  1. The server code is sending an Option<Ticket> via the response_sender, but the test expects a Ticket. This causes a type mismatch and the error related to calling .unwrap() on an Option<Ticket>.
  2. The test is calling .unwrap() on the Ticket, which causes an error because the receiver is not returning an Option<Ticket>, it should be returning Ticket directly.

Solution:

There are two key issues to address:

  1. Server code should send a Ticket directly, not an Option<Ticket>. If store.get(id) returns Some(ticket), we should send that ticket. If None is returned, we can handle the case by logging an error, returning a default ticket, or another method of error handling.
  2. The test code should not call .unwrap() on an Option<Ticket>. Since the server is expected to return a Ticket, the test code can directly receive the ticket, without unwrapping it.

Updated Server Code for the Command::Get Case:

Ok(Command::Get {
    id,
    response_sender,
}) => {
    // Retrieve the ticket by its ID
    match store.get(id) {
        Some(ticket) => {
            // Send the ticket back
            response_sender
                .send(ticket)
                .expect("Error sending ticket");
        }
        None => {
            // Handle the case where the ticket doesn't exist
            eprintln!("Ticket not found with ID: {:?}", id);
            // Optionally send a default ticket or handle the error as needed
        }
    }
}

Suggestion for Fixing the Test and Server Code:

Change the Command::Get sender type to Sender<Ticket> instead of Sender<Option<Ticket>> in the Command enum:

pub enum Command {
    Insert {
        draft: TicketDraft,
        response_sender: Sender<TicketId>,
    },
    Get {
        id: TicketId,
        response_sender: Sender<Ticket>,  // Change from Option<Ticket>
    },
}

In the test, you should no longer need to use .unwrap() because the server is returning a Ticket directly, not an Option<Ticket>. You can simply receive the Ticket from the receiver:

let ticket: Ticket = response_receiver
    .recv()
    .expect("No response received!");
assert_eq!(ticket_id, ticket.id);
assert_eq!(ticket.status, Status::ToDo);
assert_eq!(ticket.title, draft.title);
assert_eq!(ticket.description, draft.description);

Expected Behaviour:

The test should pass without errors, and the server will return a Ticket instead of an Option<Ticket>, resolving both the type mismatch and .unwrap() usage.

Steps to Reproduce:

  1. Run the test with the current code.
  2. Observe the compilation error related to .unwrap() on the Ticket type and the type mismatch when returning Option<Ticket>.
  3. Implement the suggested solution to fix the type mismatch and re-run the test.

Expected Outcome:

The test should pass without errors, and the server code will correctly handle the received ticket, ensuring that .unwrap() is removed from the test code.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant