Browse Source

Move worker limit from add_opening to fill_opening in the working group module.

Shamil Gadelshin 4 years ago
parent
commit
1cd0393e6c

+ 8 - 4
runtime-modules/working-group/src/lib.rs

@@ -516,10 +516,6 @@ decl_module! {
 
             Self::ensure_opening_human_readable_text_is_valid(&human_readable_text)?;
 
-            ensure!(
-                (Self::active_worker_count()) < T::MaxWorkerNumberLimit::get(),
-                Error::MaxActiveWorkerNumberExceeded
-            );
 
             // Add opening
             // NB: This call can in principle fail, because the staking policies
@@ -784,6 +780,14 @@ decl_module! {
 
             Self::ensure_origin_for_opening_type(origin, opening.opening_type)?;
 
+            let potential_worker_number =
+                Self::active_worker_count() + (successful_application_ids.len() as u32);
+
+            ensure!(
+                potential_worker_number <= T::MaxWorkerNumberLimit::get(),
+                Error::MaxActiveWorkerNumberExceeded
+            );
+
             // Cannot hire a lead when another leader exists.
             if matches!(opening.opening_type, OpeningType::Leader) {
                 ensure!(!<CurrentLead<T,I>>::exists(), Error::CannotHireLeaderWhenLeaderExists);

+ 1 - 1
runtime-modules/working-group/src/tests/hiring_workflow.rs

@@ -113,7 +113,7 @@ impl HiringWorkflow {
             SetLeadFixture::default().set_lead();
         }
         increase_total_balance_issuance_using_account_id(1, 10000);
-        setup_members(3);
+        setup_members(4);
         set_mint_id(create_mint());
     }
 

+ 18 - 1
runtime-modules/working-group/src/tests/mod.rs

@@ -1913,7 +1913,7 @@ fn active_worker_counter_works_successfully() {
 }
 
 #[test]
-fn adding_too_much_workers_fails() {
+fn adding_too_much_workers_fails_with_single_application_out_of_limit() {
     build_test_externalities().execute_with(|| {
         HireLeadFixture::default().hire_lead();
 
@@ -1928,3 +1928,20 @@ fn adding_too_much_workers_fails() {
         hiring_workflow.execute()
     });
 }
+
+#[test]
+fn fill_opening_cannot_hire_more_workers_using_several_applicationst_han_allows_worker_limit() {
+    build_test_externalities().execute_with(|| {
+        HireLeadFixture::default().hire_lead();
+
+        fill_worker_position(None, None, false, OpeningType::Worker, None);
+
+        let hiring_workflow = HiringWorkflow::default()
+            .disable_setup_environment()
+            .add_application_with_origin(b"Some1".to_vec(), RawOrigin::Signed(2), 2)
+            .add_application_with_origin(b"Some2".to_vec(), RawOrigin::Signed(3), 3)
+            .expect(Err(Error::MaxActiveWorkerNumberExceeded));
+
+        hiring_workflow.execute()
+    });
+}