From a4eafaa2a0fdac21ce8919e29c3e1cd43d93ef96 Mon Sep 17 00:00:00 2001 From: Pavlo Shtohryn Date: Thu, 26 Dec 2024 19:11:19 +0200 Subject: [PATCH 1/8] fix/select_top_level_subject --- pages/preprints.py | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/pages/preprints.py b/pages/preprints.py index d598173b..f113f063 100644 --- a/pages/preprints.py +++ b/pages/preprints.py @@ -1,3 +1,4 @@ +import time from urllib.parse import urljoin import pytest @@ -113,14 +114,12 @@ def select_from_dropdown_listbox(self, selection): ) def select_top_level_subject(self, selection): - for subject in self.top_level_subjects: - if subject.text == selection: - # Find the checkbox element and click it to select the subject - checkbox = subject.find_element_by_css_selector( - 'input.ember-checkbox.ember-view' - ) - checkbox.click() - break + subject = None + while subject is None: + time.sleep(1) + subject = next((subject for subject in self.top_level_subjects if subject.text == selection), None) + checkbox = subject.find_element(By.CSS_SELECTOR, 'input.ember-checkbox.ember-view') + checkbox.click() first_selected_subject = Locator(By.CSS_SELECTOR, 'li[data-test-selected-subject]') basics_tags_section = Locator(By.CSS_SELECTOR, '[data-test-no-tags]') From 032ae14cab2956c4f12684fe2fcdc4770c488206 Mon Sep 17 00:00:00 2001 From: Shtohryn Date: Wed, 15 Jan 2025 18:18:54 +0200 Subject: [PATCH 2/8] Enhance select_top_level_subject implementation --- base/expected_conditions.py | 19 +++++++++++++++++++ pages/preprints.py | 16 ++++++++++------ 2 files changed, 29 insertions(+), 6 deletions(-) diff --git a/base/expected_conditions.py b/base/expected_conditions.py index 337df656..ce76d34e 100644 --- a/base/expected_conditions.py +++ b/base/expected_conditions.py @@ -1,3 +1,5 @@ +from selenium.common.exceptions import StaleElementReferenceException +from selenium.webdriver.remote.webdriver import WebDriver from selenium.webdriver.support import expected_conditions as EC @@ -25,3 +27,20 @@ def __init__(self, page_index): def __call__(self, driver): return len(driver.window_handles) > self.page_index + + +class text_to_be_present_in_elements(object): + """ An expectation for checking if the given text is present in the + specified element. + locator, text + """ + def __init__(self, locator, text_): + self.locator = locator + self.text = text_ + + def __call__(self, driver: WebDriver): + try: + element_texts = [element.text for element in driver.find_elements(*self.locator)] + return any([self.text in element_text for element_text in element_texts]) + except StaleElementReferenceException: + return False \ No newline at end of file diff --git a/pages/preprints.py b/pages/preprints.py index f113f063..105b514e 100644 --- a/pages/preprints.py +++ b/pages/preprints.py @@ -1,10 +1,13 @@ import time from urllib.parse import urljoin +import ipdb import pytest from selenium.webdriver.common.by import By +from selenium.webdriver.support.wait import WebDriverWait import settings +from base.expected_conditions import text_to_be_present_in_elements from base.locators import ( ComponentLocator, GroupLocator, @@ -114,12 +117,13 @@ def select_from_dropdown_listbox(self, selection): ) def select_top_level_subject(self, selection): - subject = None - while subject is None: - time.sleep(1) - subject = next((subject for subject in self.top_level_subjects if subject.text == selection), None) - checkbox = subject.find_element(By.CSS_SELECTOR, 'input.ember-checkbox.ember-view') - checkbox.click() + subject_selector = 'div[data-analytics-scope="Browse"] > ul > li' + wait = WebDriverWait(self.driver, 20) + wait.until(text_to_be_present_in_elements((By.CSS_SELECTOR, subject_selector), selection)) + for subject in self.top_level_subjects: + if subject.text == selection: + subject.click() + break first_selected_subject = Locator(By.CSS_SELECTOR, 'li[data-test-selected-subject]') basics_tags_section = Locator(By.CSS_SELECTOR, '[data-test-no-tags]') From 3c272f3f7157a8ccb9e7a5053fdbdd1201021193 Mon Sep 17 00:00:00 2001 From: Shtohryn Date: Fri, 7 Feb 2025 00:59:56 +0200 Subject: [PATCH 3/8] Update due to ENG-6071 changes --- base/expected_conditions.py | 5 ++--- pages/preprints.py | 6 ++---- tests/test_preprints.py | 26 +++++++++----------------- 3 files changed, 13 insertions(+), 24 deletions(-) diff --git a/base/expected_conditions.py b/base/expected_conditions.py index ce76d34e..11594e37 100644 --- a/base/expected_conditions.py +++ b/base/expected_conditions.py @@ -40,7 +40,6 @@ def __init__(self, locator, text_): def __call__(self, driver: WebDriver): try: - element_texts = [element.text for element in driver.find_elements(*self.locator)] - return any([self.text in element_text for element_text in element_texts]) + return any(self.text in element.text for element in driver.find_elements(*self.locator)) except StaleElementReferenceException: - return False \ No newline at end of file + return False diff --git a/pages/preprints.py b/pages/preprints.py index 105b514e..44cf5783 100644 --- a/pages/preprints.py +++ b/pages/preprints.py @@ -1,7 +1,5 @@ -import time from urllib.parse import urljoin -import ipdb import pytest from selenium.webdriver.common.by import By from selenium.webdriver.support.wait import WebDriverWait @@ -118,7 +116,7 @@ def select_from_dropdown_listbox(self, selection): def select_top_level_subject(self, selection): subject_selector = 'div[data-analytics-scope="Browse"] > ul > li' - wait = WebDriverWait(self.driver, 20) + wait = WebDriverWait(self.driver, 5) wait.until(text_to_be_present_in_elements((By.CSS_SELECTOR, subject_selector), selection)) for subject in self.top_level_subjects: if subject.text == selection: @@ -128,7 +126,7 @@ def select_top_level_subject(self, selection): first_selected_subject = Locator(By.CSS_SELECTOR, 'li[data-test-selected-subject]') basics_tags_section = Locator(By.CSS_SELECTOR, '[data-test-no-tags]') basics_tags_input = Locator( - By.CSS_SELECTOR, 'input[aria-label="Add a tag to enhance discoverability"]' + By.CSS_SELECTOR, 'input[placeholder="Add a tag to enhance discoverability"]' ) # Author Assertions Page conflict_of_interest_yes = Locator( diff --git a/tests/test_preprints.py b/tests/test_preprints.py index f4c040af..9e861fb7 100644 --- a/tests/test_preprints.py +++ b/tests/test_preprints.py @@ -275,39 +275,31 @@ def test_edit_preprint(self, session, driver, preprint_detail_page): edit_page.abstract_input.send_keys('Testing Selenium Abstract edit') edit_page.next_button.click() WebDriverWait(driver, 5).until( - EC.visibility_of_element_located((By.CSS_SELECTOR, '[data-test-title]')) + EC.visibility_of_element_located((By.CSS_SELECTOR, 'h3._title_egkww5[data-test-title]')) ) - edit_page.next_button.click() - - # Next add another subject in the Discipline section - WebDriverWait(driver, 5).until( - EC.visibility_of_element_located( - ( - By.CSS_SELECTOR, - 'div.ember-tabs__tab-panel.ember-tabs__tab-panel--selected > div >ul >li>label > input', - ) - ) - ) - edit_page.scroll_into_view(edit_page.basics_tags_input.element) edit_page.select_top_level_subject('Business') - # Add another Tag and click the Save and continue button edit_page.basics_tags_input.send_keys(os.environ['PYTEST_CURRENT_TEST']) edit_page.basics_tags_input.send_keys(Keys.RETURN) # Click Return to preprint button to go back to Preprint Detail page + + body = driver.find_element(By.TAG_NAME, "body") + body.send_keys(Keys.HOME) + # Next add another subject in the Discipline section + WebDriverWait(driver, 5).until( + EC.visibility_of_element_located((By.CSS_SELECTOR, 'h3._title_egkww5')) + ) edit_page.next_button.click() WebDriverWait(driver, 5).until( EC.element_to_be_clickable((By.CSS_SELECTOR, '[data-test-next-button]')) ) - edit_page.next_button.click() WebDriverWait(driver, 5).until( EC.element_to_be_clickable((By.CSS_SELECTOR, '[data-test-next-button]')) ) - edit_page.next_button.click() WebDriverWait(driver, 5).until( - EC.element_to_be_clickable((By.CSS_SELECTOR, '[data-test-submit-button]')) + EC.element_to_be_clickable((By.CSS_SELECTOR, 'h3._title_ebg6eq')) ) edit_page.submit_preprint_button.click() detail_page = PreprintDetailPage(driver, verify=True) From d0d8731b7c4a5a8f72857de4cbb5aa35a8156031 Mon Sep 17 00:00:00 2001 From: Shtohryn Date: Fri, 14 Feb 2025 20:45:28 +0200 Subject: [PATCH 4/8] Update dynamic locators --- tests/test_preprints.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/test_preprints.py b/tests/test_preprints.py index 9e861fb7..32e47008 100644 --- a/tests/test_preprints.py +++ b/tests/test_preprints.py @@ -275,7 +275,7 @@ def test_edit_preprint(self, session, driver, preprint_detail_page): edit_page.abstract_input.send_keys('Testing Selenium Abstract edit') edit_page.next_button.click() WebDriverWait(driver, 5).until( - EC.visibility_of_element_located((By.CSS_SELECTOR, 'h3._title_egkww5[data-test-title]')) + EC.visibility_of_element_located((By.CSS_SELECTOR, '[data-test-title]')) ) edit_page.select_top_level_subject('Business') # Add another Tag and click the Save and continue button @@ -287,7 +287,7 @@ def test_edit_preprint(self, session, driver, preprint_detail_page): body.send_keys(Keys.HOME) # Next add another subject in the Discipline section WebDriverWait(driver, 5).until( - EC.visibility_of_element_located((By.CSS_SELECTOR, 'h3._title_egkww5')) + EC.visibility_of_element_located((By.CSS_SELECTOR, '[data-test-next-button]')) ) edit_page.next_button.click() WebDriverWait(driver, 5).until( @@ -299,7 +299,7 @@ def test_edit_preprint(self, session, driver, preprint_detail_page): ) edit_page.next_button.click() WebDriverWait(driver, 5).until( - EC.element_to_be_clickable((By.CSS_SELECTOR, 'h3._title_ebg6eq')) + EC.element_to_be_clickable((By.CSS_SELECTOR, '[data-test-submit-button]')) ) edit_page.submit_preprint_button.click() detail_page = PreprintDetailPage(driver, verify=True) From db27ebd7a27589da9c0a70f431e31241fcaa1511 Mon Sep 17 00:00:00 2001 From: Shtohryn Date: Mon, 3 Mar 2025 20:34:18 +0200 Subject: [PATCH 5/8] test_preprints.py stabilization --- tests/test_preprints.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/tests/test_preprints.py b/tests/test_preprints.py index 32e47008..3a934286 100644 --- a/tests/test_preprints.py +++ b/tests/test_preprints.py @@ -3,11 +3,12 @@ import re from datetime import datetime +import ipdb import pytest from selenium.common.exceptions import TimeoutException from selenium.webdriver.common.by import By from selenium.webdriver.common.keys import Keys -from selenium.webdriver.support import expected_conditions as EC +from selenium.webdriver.support import expected_conditions as EC, wait from selenium.webdriver.support.ui import WebDriverWait import markers @@ -285,7 +286,9 @@ def test_edit_preprint(self, session, driver, preprint_detail_page): body = driver.find_element(By.TAG_NAME, "body") body.send_keys(Keys.HOME) - # Next add another subject in the Discipline section + WebDriverWait(driver, 5).until( + EC.invisibility_of_element_located((By.CLASS_NAME, "toast-success")) + ) WebDriverWait(driver, 5).until( EC.visibility_of_element_located((By.CSS_SELECTOR, '[data-test-next-button]')) ) From fcf98e9268763e99e7016e3cd3a046eb8a17e15b Mon Sep 17 00:00:00 2001 From: Shtohryn Date: Tue, 4 Mar 2025 20:51:38 +0200 Subject: [PATCH 6/8] clean up --- tests/test_preprints.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/tests/test_preprints.py b/tests/test_preprints.py index 3a934286..0d7ad8d7 100644 --- a/tests/test_preprints.py +++ b/tests/test_preprints.py @@ -3,12 +3,11 @@ import re from datetime import datetime -import ipdb import pytest from selenium.common.exceptions import TimeoutException from selenium.webdriver.common.by import By from selenium.webdriver.common.keys import Keys -from selenium.webdriver.support import expected_conditions as EC, wait +from selenium.webdriver.support import expected_conditions as EC from selenium.webdriver.support.ui import WebDriverWait import markers From e94bbeca9c937e6b0a1964e0ad8b88a15b2b7fa8 Mon Sep 17 00:00:00 2001 From: Shtohryn Date: Wed, 12 Mar 2025 19:32:36 +0200 Subject: [PATCH 7/8] Pre-commit clean up --- base/expected_conditions.py | 8 +- pages/counter.java | 144 ++++++++++++++++++------------------ pages/institutions.py | 38 ++++++---- pages/preprints.py | 6 +- tests/test_institutions.py | 16 +++- tests/test_preprints.py | 8 +- 6 files changed, 124 insertions(+), 96 deletions(-) diff --git a/base/expected_conditions.py b/base/expected_conditions.py index 11594e37..ee9ffb44 100644 --- a/base/expected_conditions.py +++ b/base/expected_conditions.py @@ -30,16 +30,20 @@ def __call__(self, driver): class text_to_be_present_in_elements(object): - """ An expectation for checking if the given text is present in the + """An expectation for checking if the given text is present in the specified element. locator, text """ + def __init__(self, locator, text_): self.locator = locator self.text = text_ def __call__(self, driver: WebDriver): try: - return any(self.text in element.text for element in driver.find_elements(*self.locator)) + return any( + self.text in element.text + for element in driver.find_elements(*self.locator) + ) except StaleElementReferenceException: return False diff --git a/pages/counter.java b/pages/counter.java index aabcfbf2..beea3fce 100644 --- a/pages/counter.java +++ b/pages/counter.java @@ -1,84 +1,84 @@ -import java.util.concurrent.ExecutorService; -import java.util.concurrent.Executors; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; -/** -* @author evivehealth on 08/02/19. +/** +* @author evivehealth on 08/02/19. */ -// Java program depicting -// concurrent programming in action. +// Java program depicting +// concurrent programming in action. -// Runnable Class that defines the logic -// of run method of runnable interface -public class Counter implements Runnable -{ - private final MainApp mainApp; - private final int loopLimit; - private final String task; +// Runnable Class that defines the logic +// of run method of runnable interface +public class Counter implements Runnable +{ + private final MainApp mainApp; + private final int loopLimit; + private final String task; - // Constructor to get a reference to the main class - public Counter - (MainApp mainApp, int loopLimit, String task) - { - this.mainApp = mainApp; - this.loopLimit = loopLimit; - this.task = task; - } + // Constructor to get a reference to the main class + public Counter + (MainApp mainApp, int loopLimit, String task) + { + this.mainApp = mainApp; + this.loopLimit = loopLimit; + this.task = task; + } - // Prints the thread name, task number and - // the value of counter - // Calls pause method to allow multithreading to occur + // Prints the thread name, task number and + // the value of counter + // Calls pause method to allow multithreading to occur @Override - public void run() - { - for (int i = 0; i < loopLimit; i++) - { - System.out.println("Thread: " + + public void run() + { + for (int i = 0; i < loopLimit; i++) + { + System.out.println("Thread: " + Thread.currentThread().getName() + " Counter: " - + (i + 1) + " Task: " + task); - mainApp.pause(Math.random()); - } - } -} -class MainApp -{ + + (i + 1) + " Task: " + task); + mainApp.pause(Math.random()); + } + } +} +class MainApp +{ - // Starts the threads. Pool size 2 means at any time - // there can only be two simultaneous threads - public void startThread() - { - ExecutorService taskList = - Executors.newFixedThreadPool(2); - for (int i = 0; i < 5; i++) - { - // Makes tasks available for execution. - // At the appropriate time, calls run - // method of runnable interface - taskList.execute(new Counter(this, i + 1, - "task " + (i + 1))); - } + // Starts the threads. Pool size 2 means at any time + // there can only be two simultaneous threads + public void startThread() + { + ExecutorService taskList = + Executors.newFixedThreadPool(2); + for (int i = 0; i < 5; i++) + { + // Makes tasks available for execution. + // At the appropriate time, calls run + // method of runnable interface + taskList.execute(new Counter(this, i + 1, + "task " + (i + 1))); + } - // Shuts the thread that's watching to see if - // you have added new tasks. - taskList.shutdown(); - } + // Shuts the thread that's watching to see if + // you have added new tasks. + taskList.shutdown(); + } - // Pauses execution for a moment - // so that system switches back and forth - public void pause(double seconds) - { + // Pauses execution for a moment + // so that system switches back and forth + public void pause(double seconds) + { try - { - Thread.sleep(Math.round(1000.0 * seconds)); - } - catch (InterruptedException e) - { - e.printStackTrace(); - } - } + { + Thread.sleep(Math.round(1000.0 * seconds)); + } + catch (InterruptedException e) + { + e.printStackTrace(); + } + } - // Driver method - public static void main(String[] args) - { - new MainApp().startThread(); - } -} + // Driver method + public static void main(String[] args) + { + new MainApp().startThread(); + } +} diff --git a/pages/institutions.py b/pages/institutions.py index 97397367..3e1bd76b 100644 --- a/pages/institutions.py +++ b/pages/institutions.py @@ -60,9 +60,7 @@ class InstitutionAdminDashboardPage(BaseInstitutionPage): url_addition = '/dashboard' identity = Locator(By.CSS_SELECTOR, 'img[alt="Center For Open Science [Test]"]') - loading_indicator = Locator( - By.CSS_SELECTOR, '.ball-scale', settings.LONG_TIMEOUT - ) + loading_indicator = Locator(By.CSS_SELECTOR, '.ball-scale', settings.LONG_TIMEOUT) title_containers = GroupLocator( By.CSS_SELECTOR, '._title-container_1d9vmx', @@ -84,28 +82,40 @@ class InstitutionAdminDashboardPage(BaseInstitutionPage): By.CSS_SELECTOR, 'div._table-wrapper_1w5vdt > div > div.ember-view > div > div > table > tbody > tr', ) + def get_expanded_total_by_expanded_name(self, department): - for element in self.department_options: - name_elem = element.find_element(By.CSS_SELECTOR, "[data-test-expanded-name]") - if name_elem.text.strip() == department: - total_elem = element.find_element(By.CSS_SELECTOR, "[data-test-expanded-total]") + for element in self.department_options: + name_elem = element.find_element( + By.CSS_SELECTOR, '[data-test-expanded-name]' + ) + if name_elem.text.strip() == department: + total_elem = element.find_element( + By.CSS_SELECTOR, '[data-test-expanded-total]' + ) return int(total_elem.text.strip()) def get_kpi_data_by_kpi_title(self, target_title): for container in self.kpi_container: - title_element = container.find_element(By.CSS_SELECTOR, "[data-test-kpi-title]") + title_element = container.find_element( + By.CSS_SELECTOR, '[data-test-kpi-title]' + ) if title_element.text.strip() == target_title: - value_element = container.find_element(By.CSS_SELECTOR, "[data-test-kpi-data]") + value_element = container.find_element( + By.CSS_SELECTOR, '[data-test-kpi-data]' + ) return value_element.text.strip() def click_on_listbox_trigger(self, section_title): for section in self.title_containers: - title_element = section.find_element(By.CSS_SELECTOR, "[data-test-chart-title]") + title_element = section.find_element( + By.CSS_SELECTOR, '[data-test-chart-title]' + ) if title_element.text.strip() == section_title: - button = section.find_element(By.CSS_SELECTOR, "[data-test-expand-additional-data]") - icon = section.find_element(By.CSS_SELECTOR, "[data-test-toggle-icon]") + button = section.find_element( + By.CSS_SELECTOR, '[data-test-expand-additional-data]' + ) + icon = section.find_element(By.CSS_SELECTOR, '[data-test-toggle-icon]') button.click() WebDriverWait(self.driver, 10).until( - lambda d: icon.get_attribute("data-icon") == "caret-up" + lambda d: icon.get_attribute('data-icon') == 'caret-up' ) - diff --git a/pages/preprints.py b/pages/preprints.py index 44cf5783..a2a3358c 100644 --- a/pages/preprints.py +++ b/pages/preprints.py @@ -117,7 +117,11 @@ def select_from_dropdown_listbox(self, selection): def select_top_level_subject(self, selection): subject_selector = 'div[data-analytics-scope="Browse"] > ul > li' wait = WebDriverWait(self.driver, 5) - wait.until(text_to_be_present_in_elements((By.CSS_SELECTOR, subject_selector), selection)) + wait.until( + text_to_be_present_in_elements( + (By.CSS_SELECTOR, subject_selector), selection + ) + ) for subject in self.top_level_subjects: if subject.text == selection: subject.click() diff --git a/tests/test_institutions.py b/tests/test_institutions.py index 244dafd2..bb1ce30c 100644 --- a/tests/test_institutions.py +++ b/tests/test_institutions.py @@ -1,4 +1,5 @@ import pytest + import markers from api import osf_api from pages.institutions import ( @@ -58,7 +59,9 @@ def test_institution_admin_dashboard(self, driver, session, must_be_logged_in): api_public_project_count = metrics_data['attributes']['public_project_count'] api_private_project_count = metrics_data['attributes']['private_project_count'] - total_project_count = dashboard_page.get_kpi_data_by_kpi_title('OSF Public and Private Projects') + total_project_count = dashboard_page.get_kpi_data_by_kpi_title( + 'OSF Public and Private Projects' + ) # Verify Total User Count displayed_user_count = dashboard_page.get_kpi_data_by_kpi_title('Total Users') @@ -67,14 +70,19 @@ def test_institution_admin_dashboard(self, driver, session, must_be_logged_in): dashboard_page.click_on_listbox_trigger('Public vs Private Projects') # Verify Public Project Count - displayed_public_project_count = dashboard_page.get_expanded_total_by_expanded_name('Public Projects') + displayed_public_project_count = ( + dashboard_page.get_expanded_total_by_expanded_name('Public Projects') + ) assert int(displayed_public_project_count) == api_public_project_count # Verify Private Project Count - displayed_private_project_count = dashboard_page.get_expanded_total_by_expanded_name('Private Projects') + displayed_private_project_count = ( + dashboard_page.get_expanded_total_by_expanded_name('Private Projects') + ) assert int(displayed_private_project_count) == api_private_project_count # Verify Total Project Count assert ( - int(total_project_count) == api_public_project_count + api_private_project_count + int(total_project_count) + == api_public_project_count + api_private_project_count ) diff --git a/tests/test_preprints.py b/tests/test_preprints.py index 0d7ad8d7..2a31aec6 100644 --- a/tests/test_preprints.py +++ b/tests/test_preprints.py @@ -283,13 +283,15 @@ def test_edit_preprint(self, session, driver, preprint_detail_page): edit_page.basics_tags_input.send_keys(Keys.RETURN) # Click Return to preprint button to go back to Preprint Detail page - body = driver.find_element(By.TAG_NAME, "body") + body = driver.find_element(By.TAG_NAME, 'body') body.send_keys(Keys.HOME) WebDriverWait(driver, 5).until( - EC.invisibility_of_element_located((By.CLASS_NAME, "toast-success")) + EC.invisibility_of_element_located((By.CLASS_NAME, 'toast-success')) ) WebDriverWait(driver, 5).until( - EC.visibility_of_element_located((By.CSS_SELECTOR, '[data-test-next-button]')) + EC.visibility_of_element_located( + (By.CSS_SELECTOR, '[data-test-next-button]') + ) ) edit_page.next_button.click() WebDriverWait(driver, 5).until( From f637a4c1ff0756d3a2f36df0c962cc107a21f215 Mon Sep 17 00:00:00 2001 From: Shtohryn Date: Wed, 12 Mar 2025 19:34:27 +0200 Subject: [PATCH 8/8] Delete pages/counter.java --- pages/counter.java | 84 ---------------------------------------------- 1 file changed, 84 deletions(-) delete mode 100644 pages/counter.java diff --git a/pages/counter.java b/pages/counter.java deleted file mode 100644 index beea3fce..00000000 --- a/pages/counter.java +++ /dev/null @@ -1,84 +0,0 @@ -import java.util.concurrent.ExecutorService; -import java.util.concurrent.Executors; - -/** -* @author evivehealth on 08/02/19. -*/ -// Java program depicting -// concurrent programming in action. - -// Runnable Class that defines the logic -// of run method of runnable interface -public class Counter implements Runnable -{ - private final MainApp mainApp; - private final int loopLimit; - private final String task; - - // Constructor to get a reference to the main class - public Counter - (MainApp mainApp, int loopLimit, String task) - { - this.mainApp = mainApp; - this.loopLimit = loopLimit; - this.task = task; - } - - // Prints the thread name, task number and - // the value of counter - // Calls pause method to allow multithreading to occur - @Override - public void run() - { - for (int i = 0; i < loopLimit; i++) - { - System.out.println("Thread: " + - Thread.currentThread().getName() + " Counter: " - + (i + 1) + " Task: " + task); - mainApp.pause(Math.random()); - } - } -} -class MainApp -{ - - // Starts the threads. Pool size 2 means at any time - // there can only be two simultaneous threads - public void startThread() - { - ExecutorService taskList = - Executors.newFixedThreadPool(2); - for (int i = 0; i < 5; i++) - { - // Makes tasks available for execution. - // At the appropriate time, calls run - // method of runnable interface - taskList.execute(new Counter(this, i + 1, - "task " + (i + 1))); - } - - // Shuts the thread that's watching to see if - // you have added new tasks. - taskList.shutdown(); - } - - // Pauses execution for a moment - // so that system switches back and forth - public void pause(double seconds) - { - try - { - Thread.sleep(Math.round(1000.0 * seconds)); - } - catch (InterruptedException e) - { - e.printStackTrace(); - } - } - - // Driver method - public static void main(String[] args) - { - new MainApp().startThread(); - } -}