Repository navigation
feat: add load_data_fun to load dynamic data #107
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -838,6 +838,50 @@ defmodule RustlerPrecompiledTest do | |
| end | ||
| end | ||
|
|
||
| @tag :tmp_dir | ||
| test "use RustlerPrecompiled calls `load_data_fun` every time the module is loaded", %{ | ||
| tmp_dir: tmp_dir | ||
| } do | ||
| nif_fixtures_dir = Path.join(File.cwd!(), "test/fixtures") | ||
| checksum_sample = File.read!(Path.join(nif_fixtures_dir, "checksum-sample-file.exs")) | ||
| Process.register(self(), :load_data_fun_test) | ||
|
|
||
| in_tmp(tmp_dir, fn -> | ||
| File.write!("checksum-Elixir.RustlerPrecompilationExample.Native.exs", checksum_sample) | ||
|
|
||
| {[{module, binary}], _log} = | ||
| with_log(fn -> | ||
| Code.compile_quoted( | ||
| quote do | ||
| defmodule RustlerPrecompilationExample.Native do | ||
| use RustlerPrecompiled, | ||
| otp_app: :rustler_precompiled, | ||
| crate: "example", | ||
| base_url: | ||
| "https://github.com/philss/rustler_precompilation_example/releases/download/v0.2.0", | ||
| base_cache_dir: unquote(nif_fixtures_dir), | ||
| version: "0.2.0", | ||
| force_build: false, | ||
| load_data_fun: {RustlerPrecompiledTest.LoadData, :load_data} | ||
|
|
||
| def add(_a, _b), do: :erlang.nif_error(:nif_not_loaded) | ||
| end | ||
| end | ||
| ) | ||
| end) | ||
|
|
||
| assert_receive :load_data_called | ||
| assert module.add(1, 2) == 3 | ||
|
|
||
| # Loading the same binary again calls it again, | ||
| # so the data is not fixed at compile time. | ||
| :code.delete(module) | ||
| :code.purge(module) | ||
| assert {:module, ^module} = :code.load_binary(module, ~c"nofile", binary) | ||
| assert_receive :load_data_called | ||
| end) | ||
| end | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @leandrocp This test is giving a Please give it a try, but if you can't, I'm comfortable with proceeding without this integration test.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hey @philss I was able to reproduce on a Linux container (it works on my local Mac). I think the root cause was caused by 051306b where it added the archive extension so Note that 6e7d7bc matches
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I see, and I agree it looks correct. Would you mind to open a isolated PR with that commit? I think it would be better for documenting the fix. We can rebase it after the merge.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not a problem! Done #109
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's odd that we don't have a conflict here, but please rebase so I can merge with only the changes for the
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I applied the same patch in #109 and just added one test so I think that wouldn't create a conflict :) |
||
|
|
||
| describe "build_metadata/1" do | ||
| test "builds a valid metadata" do | ||
| config = | ||
|
|
@@ -1218,6 +1262,13 @@ defmodule RustlerPrecompiledTest do | |
| end | ||
| end | ||
|
|
||
| defmodule LoadData do | ||
| def load_data do | ||
| send(:load_data_fun_test, :load_data_called) | ||
| :load_data | ||
| end | ||
| end | ||
|
|
||
| def in_tmp(tmp_path, function) do | ||
| path = Path.join([tmp_path, random_string(10)]) | ||
|
|
||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.