Skip to content

Commit

Permalink
wip: compiling, but specs do not pass
Browse files Browse the repository at this point in the history
  • Loading branch information
caspiano committed Jul 21, 2022
1 parent 7121021 commit fd1c903
Show file tree
Hide file tree
Showing 10 changed files with 163 additions and 129 deletions.
60 changes: 31 additions & 29 deletions spec/processes/edge_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ module PlaceOS::Core::ProcessManager
module : Model::Module,
edge : Model::Edge,
driver_path : String,
driver_key : String
driver_key : String,
driver_id : String

class_getter binary_store : Build::Filesystem { Build::Filesystem.new(Dir.tempdir) }

Expand All @@ -27,7 +28,7 @@ module PlaceOS::Core::ProcessManager
end

def self.with_edge
with_driver do |mod, driver_path, driver_key, _driver|
with_driver do |mod, driver_path, driver_key, driver|
if (existing_edge_id = mod.edge_id)
mod.running = false
mod.save!
Expand All @@ -44,6 +45,7 @@ module PlaceOS::Core::ProcessManager
edge: edge,
driver_path: driver_path,
driver_key: driver_key,
driver_id: driver.id.as(String),
)

client, process_manager = client_server(edge.id.as(String))
Expand All @@ -56,7 +58,7 @@ module PlaceOS::Core::ProcessManager
it "debug" do
with_edge do |ctx, _client, pm|
module_id = ctx.module.id.as(String)
pm.load(module_id: module_id, driver_key: ctx.driver_key)
pm.load(module_id: module_id, driver_key: ctx.driver_key, driver_id: ctx.driver_id)
pm.start(module_id: module_id, payload: Resources::Modules.start_payload(ctx.module))

message_channel = Channel(String).new
Expand All @@ -82,27 +84,27 @@ module PlaceOS::Core::ProcessManager
describe "driver_loaded?" do
it "confirms a driver is loaded" do
with_edge do |ctx, client, pm|
pm.load(module_id: "mod", driver_key: ctx.driver_key)
client.driver_loaded?(ctx.driver_key).should be_true
pm.driver_loaded?(ctx.driver_key).should be_true
pm.load(module_id: "mod", driver_key: ctx.driver_key, driver_id: ctx.driver_id)
client.driver_loaded?(ctx.driver_key, ctx.driver_id).should be_true
pm.driver_loaded?(ctx.driver_key, ctx.driver_id).should be_true
end
end

it "confirms a driver is not loaded" do
with_edge do |_ctx, client, pm|
pm.driver_loaded?("does-not-exist").should be_false
client.driver_loaded?("does-not-exist").should be_false
pm.driver_loaded?("does-not-exist", "driver-123").should be_false
client.driver_loaded?("does-not-exist", "driver-123").should be_false
end
end
end

describe "driver_status" do
it "returns driver status if present" do
with_edge do |ctx, client, pm|
pm.load(module_id: "mod", driver_key: ctx.driver_key)
pm.load(module_id: "mod", driver_key: ctx.driver_key, driver_id: ctx.driver_id)

pm.driver_status(ctx.driver_key).should_not be_nil
status = client.driver_status(ctx.driver_key)
pm.driver_status(ctx.driver_key, ctx.driver_id).should_not be_nil
status = client.driver_status(ctx.driver_key, ctx.driver_id)
status.should_not be_nil
status.not_nil!.running.should be_false
status.not_nil!.launch_count.should eq(0)
Expand All @@ -111,16 +113,16 @@ module PlaceOS::Core::ProcessManager

it "returns nil in not present" do
with_edge do |_ctx, client, pm|
pm.driver_status("doesntexist").should be_nil
client.driver_status("doesntexist").should be_nil
pm.driver_status("doesntexist", "driver-123").should be_nil
client.driver_status("doesntexist", "driver-123").should be_nil
end
end
end

it "execute" do
with_edge do |ctx, _client, pm|
module_id = ctx.module.id.as(String)
pm.load(module_id: module_id, driver_key: ctx.driver_key)
pm.load(module_id: module_id, driver_key: ctx.driver_key, driver_id: ctx.driver_id)
pm.start(module_id: module_id, payload: Resources::Modules.start_payload(ctx.module))
result, code = pm.execute(module_id: module_id, payload: Resources::Modules.execute_payload(:used_for_place_testing), user_id: nil)
result.should eq %("you can delete this file")
Expand All @@ -131,7 +133,7 @@ module PlaceOS::Core::ProcessManager
it "ignore" do
with_edge do |ctx, _client, pm|
module_id = ctx.module.id.as(String)
pm.load(module_id: module_id, driver_key: ctx.driver_key)
pm.load(module_id: module_id, driver_key: ctx.driver_key, driver_id: ctx.driver_id)
pm.start(module_id: module_id, payload: Resources::Modules.start_payload(ctx.module))
message_channel = Channel(String).new

Expand Down Expand Up @@ -196,14 +198,14 @@ module PlaceOS::Core::ProcessManager

it "load" do
with_edge do |ctx, client, pm|
pm.driver_loaded?(ctx.driver_key).should be_false
pm.driver_loaded?(ctx.driver_key, ctx.driver_id).should be_false
pm.module_loaded?("mod").should be_false
client.module_loaded?("mod").should be_false

pm.load(module_id: "mod", driver_key: ctx.driver_key)
pm.load(module_id: "mod", driver_key: ctx.driver_key, driver_id: ctx.driver_id)

pm.driver_loaded?(ctx.driver_key).should be_true
client.driver_loaded?(ctx.driver_key).should be_true
pm.driver_loaded?(ctx.driver_key, ctx.driver_id).should be_true
client.driver_loaded?(ctx.driver_key, ctx.driver_id).should be_true

pm.module_loaded?("mod").should be_true
client.module_loaded?("mod").should be_true
Expand All @@ -219,7 +221,7 @@ module PlaceOS::Core::ProcessManager
describe "module_loaded?" do
it "confirms a module is loaded" do
with_edge do |ctx, _client, pm|
pm.load(module_id: "mod", driver_key: ctx.driver_key)
pm.load(module_id: "mod", driver_key: ctx.driver_key, driver_id: ctx.driver_id)
pm.module_loaded?("mod").should be_true
end
end
Expand All @@ -233,7 +235,7 @@ module PlaceOS::Core::ProcessManager

it "run_count" do
with_edge do |ctx, _client, pm|
pm.load(module_id: "mod", driver_key: ctx.driver_key)
pm.load(module_id: "mod", driver_key: ctx.driver_key, driver_id: ctx.driver_id)
pm.run_count.should eq(ProcessManager::Count.new(1, 1))
end
end
Expand All @@ -247,7 +249,7 @@ module PlaceOS::Core::ProcessManager
it "start" do
with_edge do |ctx, client, pm|
module_id = ctx.module.id.as(String)
pm.load(module_id: module_id, driver_key: ctx.driver_key)
pm.load(module_id: module_id, driver_key: ctx.driver_key, driver_id: ctx.driver_id)
pm.start(module_id: module_id, payload: Resources::Modules.start_payload(ctx.module))
pm.loaded_modules.should eq({ctx.driver_key => [module_id]})
client.loaded_modules.should eq({ctx.driver_key => [module_id]})
Expand Down Expand Up @@ -283,11 +285,11 @@ module PlaceOS::Core::ProcessManager
path = (Path[ctx.driver_path].parent / key).to_s
File.copy(ctx.driver_path, path)

pm.load(module_id: module_id, driver_key: key)
pm.driver_loaded?(path).should be_true
pm.load(module_id: module_id, driver_key: key, driver_id: ctx.driver_id)
pm.driver_loaded?(path, ctx.driver_id).should be_true
pm.module_loaded?(module_id).should be_true
pm.unload(module_id)
pm.driver_loaded?(path).should be_false
pm.driver_loaded?(path, ctx.driver_id).should be_false
pm.module_loaded?(module_id).should be_false
end
end
Expand All @@ -297,15 +299,15 @@ module PlaceOS::Core::ProcessManager
module0 = "mod0"
module1 = "mod1"

pm.load(module_id: module0, driver_key: ctx.driver_key)
pm.load(module_id: module1, driver_key: ctx.driver_key)
pm.driver_loaded?(ctx.driver_key).should be_true
pm.load(module_id: module0, driver_key: ctx.driver_key, driver_id: ctx.driver_id)
pm.load(module_id: module1, driver_key: ctx.driver_key, driver_id: ctx.driver_id)
pm.driver_loaded?(ctx.driver_key, ctx.driver_id).should be_true
pm.module_loaded?(module0).should be_true
pm.module_loaded?(module1).should be_true
pm.unload(module0)
pm.module_loaded?(module0).should be_false
pm.module_loaded?(module1).should be_true
pm.driver_loaded?(ctx.driver_key).should be_true
pm.driver_loaded?(ctx.driver_key, ctx.driver_id).should be_true
end
end
end
Expand Down
62 changes: 35 additions & 27 deletions spec/processes/local_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -22,44 +22,47 @@ module PlaceOS::Core::ProcessManager

def self.test_starting(manager, mod, driver_key)
module_id = mod.id.as(String)
manager.load(module_id: module_id, driver_key: driver_key)
driver_id = mod.driver!.id.as(String)
manager.load(module_id: module_id, driver_key: driver_key, driver_id: driver_id)
manager.start(module_id: module_id, payload: Resources::Modules.start_payload(mod))
manager.loaded_modules.should eq({driver_key => [module_id]})
manager.loaded_modules.should eq({ProcessManager.driver_scoped_name(driver_key, driver_id) => [module_id]})
end

describe Local, tags: "processes" do
with_driver do |mod, driver_path, driver_key, _driver|
with_driver do |mod, driver_path, driver_key, driver|
describe Local::Common do
describe "driver_loaded?" do
it "confirms a driver is loaded" do
driver_id = driver.id.as(String)
pm = local
pm.load(module_id: "mod", driver_key: driver_key)
pm.driver_loaded?(driver_key).should be_true
pm.load(module_id: "mod", driver_key: driver_key, driver_id: driver_id)
pm.driver_loaded?(driver_key, driver_id).should be_true
end

it "confirms a driver is not loaded" do
local.driver_loaded?("does-not-exist").should be_false
local.driver_loaded?("does-not-exist", "driver-123").should be_false
end
end

describe "driver_status" do
it "returns driver status if present" do
pm = local
pm.load(module_id: "mod", driver_key: driver_key)
driver_id = driver.id.as(String)
pm.load(module_id: "mod", driver_key: driver_key, driver_id: driver_id)

status = pm.driver_status(driver_key)
status = pm.driver_status(driver_key, driver_id)
status.should_not be_nil
end

it "returns nil in not present" do
local.driver_status("doesntexist").should be_nil
local.driver_status("doesntexist", "driver-1234").should be_nil
end
end

it "execute" do
pm = local
module_id = mod.id.as(String)
pm.load(module_id: module_id, driver_key: driver_key)
pm.load(module_id: module_id, driver_key: driver_key, driver_id: driver.id.as(String))
pm.start(module_id: module_id, payload: Resources::Modules.start_payload(mod))
result, code = pm.execute(module_id: module_id, payload: Resources::Modules.execute_payload(:used_for_place_testing), user_id: nil)
result.should eq %("you can delete this file")
Expand All @@ -69,7 +72,7 @@ module PlaceOS::Core::ProcessManager
it "debug" do
pm = local
module_id = mod.id.as(String)
pm.load(module_id: module_id, driver_key: driver_key)
pm.load(module_id: module_id, driver_key: driver_key, driver_id: driver.id.as(String))
pm.start(module_id: module_id, payload: Resources::Modules.start_payload(mod))
message_channel = Channel(String).new

Expand All @@ -93,7 +96,7 @@ module PlaceOS::Core::ProcessManager
it "ignore" do
pm = local
module_id = mod.id.as(String)
pm.load(module_id: module_id, driver_key: driver_key)
pm.load(module_id: module_id, driver_key: driver_key, driver_id: driver.id.as(String))
pm.start(module_id: module_id, payload: Resources::Modules.start_payload(mod))
message_channel = Channel(String).new

Expand Down Expand Up @@ -131,9 +134,10 @@ module PlaceOS::Core::ProcessManager
it "start" do
pm = local
module_id = mod.id.as(String)
pm.load(module_id: module_id, driver_key: driver_key)
driver_id = driver.id.as(String)
pm.load(module_id: module_id, driver_key: driver_key, driver_id: driver_id)
pm.start(module_id: module_id, payload: Resources::Modules.start_payload(mod))
pm.loaded_modules.should eq({driver_key => [module_id]})
pm.loaded_modules.should eq({ProcessManager.driver_scoped_name(driver_key, driver_id) => [module_id]})
pm.kill(driver_key)
end

Expand All @@ -142,9 +146,10 @@ module PlaceOS::Core::ProcessManager
pm.kill(driver_key)
test_starting(pm, mod, driver_key)
pm.stop(mod.id.as(String))
driver_id = mod.driver!.id.as(String)

sleep 0.1
pm.loaded_modules.should eq({driver_key => [] of String})
pm.loaded_modules.should eq({ProcessManager.driver_scoped_name(driver_key, driver_id) => [] of String})
end

it "system_status" do
Expand Down Expand Up @@ -185,7 +190,7 @@ module PlaceOS::Core::ProcessManager
describe "module_loaded?" do
it "confirms a module is loaded" do
pm = local
pm.load(module_id: "mod", driver_key: driver_key)
pm.load(module_id: "mod", driver_key: driver_key, driver_id: driver.id.as(String))
pm.module_loaded?("mod").should be_true
end

Expand All @@ -196,25 +201,26 @@ module PlaceOS::Core::ProcessManager

it "run_count" do
pm = local
pm.load(module_id: "mod", driver_key: driver_key)
pm.load(module_id: "mod", driver_key: driver_key, driver_id: driver.id.as(String))
pm.run_count.should eq(ProcessManager::Count.new(1, 1))
end

describe "unload" do
it "removes driver if no dependent modules running" do
module_id = "mod"
driver_id = driver.id.as(String)

rand_string = UUID.random.to_s.delete('-')
key = rand_string + driver_key
path = (Path[driver_path].parent / key).to_s
File.copy(driver_path, path)

pm = local
pm.load(module_id: module_id, driver_key: key)
pm.driver_loaded?(path).should be_true
pm.load(module_id: module_id, driver_key: key, driver_id: driver_id)
pm.driver_loaded?(path, driver_id).should be_true
pm.module_loaded?(module_id).should be_true
pm.unload(module_id)
pm.driver_loaded?(path).should be_false
pm.driver_loaded?(path, driver_id).should be_false
pm.module_loaded?(module_id).should be_false
File.exists?(path).should be_true

Expand All @@ -227,20 +233,21 @@ module PlaceOS::Core::ProcessManager
path = (Path[driver_path].parent / key).to_s
File.copy(driver_path, path)

driver_id = driver.id.as(String)
module0 = "mod0"
module1 = "mod1"
File.copy(driver_path, path)

pm = local
pm.load(module_id: module0, driver_key: key)
pm.load(module_id: module1, driver_key: key)
pm.driver_loaded?(key).should be_true
pm.load(module_id: module0, driver_key: key, driver_id: driver_id)
pm.load(module_id: module1, driver_key: key, driver_id: driver_id)
pm.driver_loaded?(key, driver_id).should be_true
pm.module_loaded?(module0).should be_true
pm.module_loaded?(module1).should be_true
pm.unload(module0)
pm.module_loaded?(module0).should be_false
pm.module_loaded?(module1).should be_true
pm.driver_loaded?(key).should be_true
pm.driver_loaded?(key, driver_id).should be_true
File.exists?(path).should be_true

File.delete(path) rescue nil
Expand All @@ -249,11 +256,12 @@ module PlaceOS::Core::ProcessManager
end

it "load" do
driver_id = driver.id.as(String)
pm = local
pm.driver_loaded?(driver_key).should be_false
pm.driver_loaded?(driver_key, driver_id).should be_false
pm.module_loaded?("mod").should be_false
pm.load(module_id: "mod", driver_key: driver_key)
pm.driver_loaded?(driver_key).should be_true
pm.load(module_id: "mod", driver_key: driver_key, driver_id: driver_id)
pm.driver_loaded?(driver_key, driver_id).should be_true
pm.module_loaded?("mod").should be_true
end

Expand Down
Loading

0 comments on commit fd1c903

Please sign in to comment.