Skip to content
This repository has been archived by the owner on Jan 8, 2025. It is now read-only.

Commit

Permalink
3705 - switch to tested file-system primitives
Browse files Browse the repository at this point in the history
  • Loading branch information
akkartik committed Dec 12, 2016
1 parent d5c86df commit 294b2ab
Show file tree
Hide file tree
Showing 35 changed files with 1,598 additions and 1,504 deletions.
65 changes: 57 additions & 8 deletions 088file.mu
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# Wrappers around file system primitives that take a 'resources' object and
# are thus easier to test.
#
# - start-reading - asynchronously open a file, returning a channel source for
# receiving the results
# - start-writing - asynchronously open a file, returning a channel sink for
# the data to write
# - slurp - synchronously read from a file
# - dump - synchronously write to a file

container resources [
lock:bool
Expand All @@ -11,25 +18,42 @@ container resource [
contents:text
]

def start-reading resources:&:resources, filename:text -> contents:&:source:char [
def start-reading resources:&:resources, filename:text -> contents:&:source:char, error?:bool [
local-scope
load-ingredients
error? <- copy 0/false
{
break-unless resources
# fake file system
contents <- start-reading-from-fake-resources resources, filename
contents, error? <- start-reading-from-fake-resource resources, filename
return
}
# real file system
file:num <- $open-file-for-reading filename
assert file, [file not found]
return-unless file, 0/contents, 1/error?
contents:&:source:char, sink:&:sink:char <- new-channel 30
start-running receive-from-file file, sink
]

def start-reading-from-fake-resources resources:&:resources, resource:text -> contents:&:source:char [
def slurp resources:&:resources, filename:text -> contents:text, error?:bool [
local-scope
load-ingredients
source:&:source:char, error?:bool <- start-reading resources, filename
return-if error?, 0/contents
buf:&:buffer <- new-buffer 30/capacity
{
c:char, done?:bool, source <- read source
break-if done?
buf <- append buf, c
loop
}
contents <- buffer-to-array buf
]

def start-reading-from-fake-resource resources:&:resources, resource:text -> contents:&:source:char, error?:bool [
local-scope
load-ingredients
error? <- copy 0/no-error
i:num <- copy 0
data:&:@:resource <- get *resources, data:offset
len:num <- length *data
Expand All @@ -46,7 +70,7 @@ def start-reading-from-fake-resources resources:&:resources, resource:text -> co
start-running receive-from-text curr-contents, sink
return
}
return 0/not-found
return 0/not-found, 1/error
]

def receive-from-file file:num, sink:&:sink:char -> sink:&:sink:char [
Expand Down Expand Up @@ -78,18 +102,20 @@ def receive-from-text contents:text, sink:&:sink:char -> sink:&:sink:char [
sink <- close sink
]

def start-writing resources:&:resources, filename:text -> sink:&:sink:char, routine-id:num [
def start-writing resources:&:resources, filename:text -> sink:&:sink:char, routine-id:num, error?:bool [
local-scope
load-ingredients
error? <- copy 0/false
source:&:source:char, sink:&:sink:char <- new-channel 30
{
break-unless resources
# fake file system
routine-id <- start-running transmit-to-fake-file resources, filename, source
routine-id <- start-running transmit-to-fake-resource resources, filename, source
return
}
# real file system
file:num <- $open-file-for-writing filename
return-unless file, 0/sink, 0/routine-id, 1/error?
{
break-if file
msg:text <- append [no such file: ] filename
Expand All @@ -98,6 +124,29 @@ def start-writing resources:&:resources, filename:text -> sink:&:sink:char, rout
routine-id <- start-running transmit-to-file file, source
]

def dump resources:&:resources, filename:text, contents:text -> resources:&:resources, error?:bool [
local-scope
load-ingredients
# todo: really create an empty file
return-unless contents, resources, 0/no-error
sink-file:&:sink:char, write-routine:num, error?:bool <- start-writing resources, filename
return-if error?
i:num <- copy 0
len:num <- length *contents
{
done?:bool <- greater-or-equal i, len
break-if done?
c:char <- index *contents, i
sink-file <- write sink-file, c
i <- add i, 1
loop
}
close sink-file
# make sure to wait for the file to be actually written to disk
# (Mu practices structured concurrency: http://250bpm.com/blog:71)
wait-for-routine write-routine
]

def transmit-to-file file:num, source:&:source:char -> source:&:source:char [
local-scope
load-ingredients
Expand All @@ -110,7 +159,7 @@ def transmit-to-file file:num, source:&:source:char -> source:&:source:char [
file <- $close-file file
]

def transmit-to-fake-file resources:&:resources, filename:text, source:&:source:char -> resources:&:resources, source:&:source:char [
def transmit-to-fake-resource resources:&:resources, filename:text, source:&:source:char -> resources:&:resources, source:&:source:char [
local-scope
load-ingredients
lock:location <- get-location *resources, lock:offset
Expand Down
14 changes: 0 additions & 14 deletions 090scenario_filesystem_test.mu
Original file line number Diff line number Diff line change
Expand Up @@ -97,17 +97,3 @@ scenario write-to-existing-file-preserves-other-files [
11 <- 1 # other files also continue to persist unchanged
]
]

def slurp resources:&:resources, filename:text -> contents:text [
local-scope
load-ingredients
source:&:source:char <- start-reading resources, filename
buf:&:buffer <- new-buffer 30/capacity
{
c:char, done?:bool, source <- read source
break-if done?
buf <- append buf, c
loop
}
contents <- buffer-to-array buf
]
2 changes: 1 addition & 1 deletion 092socket.mu
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ def start-reading-from-network resources:&:resources, uri:text -> contents:&:sou
{
break-unless resources
# fake network
contents <- start-reading-from-fake-resources resources, uri
contents <- start-reading-from-fake-resource resources, uri
return
}
# real network
Expand Down
2 changes: 1 addition & 1 deletion 101run_sandboxed.cc
Original file line number Diff line number Diff line change
Expand Up @@ -427,8 +427,8 @@ int trace_error_contents() {
if (*--p->contents.end() != '\n') out << '\n';
}
string result = out.str();
if (result.empty()) return 0;
truncate(result);
if (result.empty()) return 0;
return new_mu_text(result);
}

Expand Down
118 changes: 0 additions & 118 deletions 102persist.cc

This file was deleted.

2 changes: 1 addition & 1 deletion cannot_write_tests_for
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
4. hide/show screen
5. more touch event types
6. sandbox isolation
7. read/write files
7. errors in reading/writing files (missing directory, others?)

termbox issues are implementation-specific and not worth testing:
whether we clear junk from other processes
Expand Down
Loading

0 comments on commit 294b2ab

Please sign in to comment.