Skip to content

Wonky error when missing newlines between messages #59

@hashimmm

Description

@hashimmm

I tried the "echo" challenge from the fly-io site. Good idea to have a simple echo things to sort out the basic setup.

So one program I tried was the following Racket code:

#lang racket/base

(require json)
(require racket/match)
(require racket/port)

(define (main)
  (for ([h (in-producer read-json eof)])
    (match h
      [(hash-table ('dest dest)
                   ('src src)
                   ('body body)
                   (k v) ...)
       (match body
         [(hash-table ('type "echo")
                      ('msg_id msg_id)
                      (bk bv))
          (write-json
           (hash
            'src dest
            'dest src
            'body (hash 'type "echo_ok"
                        'msg_id msg_id
                        'in_reply_to msg_id
                        bk bv)))]
         [(hash-table ('type "init")
                      ('msg_id msg_id)
                      (bk bv) ...)
          (write-json
           (hash 'src dest
                 'dest src
                 'body
                 (hash 'type "init_ok"
                       'in_reply_to msg_id)))]
         [_
          #f])]
      [_
       #f])
    (flush-output)))

(module+ main
  (main))

The above code results in the following error:

java.lang.AssertionError: Assert failed: Invalid dest for message #maelstrom.net.message.Message{:id 1, :src "n0", :dest "c0", :body {:in_reply_to 1, :type "init_ok"}}

I sorta guessed by reading the spec that making my code emit a newline after each message would follow the stated spec better and might just work. And it did! Simply adding (displayln "") before (flush-output) in the above code makes it work and pass the tests.

The way to run the above code is, assuming Racket 8.8 is installed, to generate an executable using raco exe echo.rkt, then running the resulting echo binary with maelstrom.

PS I don't feel too strongly about this thing and wouldn't mind if the issue is closed as unimportant

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions