Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions casbin/enforcer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -390,12 +390,13 @@ void Enforcer::ClearPolicy() {

// LoadPolicy reloads the policy from file/database.
void Enforcer::LoadPolicy() {
this->ClearPolicy();
// must use base's LoadPolicy to avoid dead lock
Enforcer::ClearPolicy();
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good work on debugging!

m_adapter->LoadPolicy(m_model);
m_model->PrintPolicy();

if(m_auto_build_role_links) {
this->BuildRoleLinks();
Enforcer::BuildRoleLinks();
}
}

Expand Down
77 changes: 39 additions & 38 deletions tests/enforcer_synced_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,57 +18,58 @@

#include <gtest/gtest.h>
#include <casbin/casbin.h>
#include "config_path.h"

namespace {

// void TestSyncFn(casbin::SyncedEnforcer& e, const std::string& sub, const std::string& obj, const std::string& act, bool control) {
// bool response = e.Enforce({ sub, obj, act });
// ASSERT_EQ(response, control);
// }
void TestSyncFn(casbin::SyncedEnforcer& e, const std::string& sub, const std::string& obj, const std::string& act, bool control) {
bool response = e.Enforce({ sub, obj, act });
ASSERT_EQ(response, control);
}

// TEST(TestEnforcerSynced, TestSync) {
// casbin::SyncedEnforcer e(basic_model_path, basic_policy_path);
TEST(TestEnforcerSynced, TestSync) {
casbin::SyncedEnforcer e(basic_model_path, basic_policy_path);

// using namespace std::literals::chrono_literals;
// auto time1 = 200ms;
// e.StartAutoLoadPolicy(time1);
using namespace std::literals::chrono_literals;
auto time1 = 200ms;
e.StartAutoLoadPolicy(time1);

// TestSyncFn(e, "alice", "data1", "read", true);
// TestSyncFn(e, "alice", "data1", "write", false);
// TestSyncFn(e, "alice", "data2", "read", false);
// TestSyncFn(e, "alice", "data2", "write", false);
// TestSyncFn(e, "bob", "data1", "read", false);
// TestSyncFn(e, "bob", "data1", "write", false);
// TestSyncFn(e, "bob", "data2", "read", false);
// TestSyncFn(e, "bob", "data2", "write", true);
TestSyncFn(e, "alice", "data1", "read", true);
TestSyncFn(e, "alice", "data1", "write", false);
TestSyncFn(e, "alice", "data2", "read", false);
TestSyncFn(e, "alice", "data2", "write", false);
TestSyncFn(e, "bob", "data1", "read", false);
TestSyncFn(e, "bob", "data1", "write", false);
TestSyncFn(e, "bob", "data2", "read", false);
TestSyncFn(e, "bob", "data2", "write", true);

// std::this_thread::sleep_for(200ms);
// e.StopAutoLoadPolicy();
// }
std::this_thread::sleep_for(200ms);
e.StopAutoLoadPolicy();
}

// TEST(TestEnforcerSynced, TestStopLoadPolicy) {
// casbin::SyncedEnforcer e(basic_model_path, basic_policy_path);
TEST(TestEnforcerSynced, TestStopLoadPolicy) {
casbin::SyncedEnforcer e(basic_model_path, basic_policy_path);

// using namespace std::literals::chrono_literals;
// std::chrono::duration<int64_t, std::nano> t = 5ms;
using namespace std::literals::chrono_literals;
std::chrono::duration<int64_t, std::nano> t = 5ms;

// e.StartAutoLoadPolicy(t);
e.StartAutoLoadPolicy(t);

// EXPECT_EQ(e.IsAutoLoadingRunning(), true);
EXPECT_EQ(e.IsAutoLoadingRunning(), true);

// TestSyncFn(e , "alice", "data1", "read", true);
// TestSyncFn(e , "alice", "data1", "write", false);
// TestSyncFn(e , "alice", "data2", "read", false);
// TestSyncFn(e , "alice", "data2", "write", false);
// TestSyncFn(e , "bob", "data1", "read", false);
// TestSyncFn(e , "bob", "data1", "write", false);
// TestSyncFn(e , "bob", "data2", "read", false);
// TestSyncFn(e , "bob", "data2", "write", true);
TestSyncFn(e , "alice", "data1", "read", true);
TestSyncFn(e , "alice", "data1", "write", false);
TestSyncFn(e , "alice", "data2", "read", false);
TestSyncFn(e , "alice", "data2", "write", false);
TestSyncFn(e , "bob", "data1", "read", false);
TestSyncFn(e , "bob", "data1", "write", false);
TestSyncFn(e , "bob", "data2", "read", false);
TestSyncFn(e , "bob", "data2", "write", true);

// e.StopAutoLoadPolicy();
// std::this_thread::sleep_for(10ms);
e.StopAutoLoadPolicy();
std::this_thread::sleep_for(10ms);

// EXPECT_EQ(e.IsAutoLoadingRunning(), false);
// }
EXPECT_EQ(e.IsAutoLoadingRunning(), false);
}

} // namespace