Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8117511
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T04:11:41+00:00 2026-06-06T04:11:41+00:00

Problem I have a UDPlistener application that I need to write a unit test

  • 0

Problem

I have a UDPlistener application that I need to write a unit test for. This listener continuously listens on a port and is meant to always be running on the product. We use the poco libraries for frameworks not in the standard library.

Now I need to add it to the unit test application.

Curent Solution

I thought it would be easiest to implement Poco::Runnable in a class RunApp that runs the application. Then I can create a new Poco::Thread in my unit test to run the RunApp class.

This works; my listener is running and I can send test messages in the unit test body after the thread is spawned. BUT, I need to stop the listener so other unit tests can run. I added a UDP message that tells the listener to kill itself but this is only used by the unit test and a potential security problem.

Question

Is there a way to force a Poco::Thread to stop? Or I structuring this unit test wrong? I don’t want the listener to run during all the other unit tests.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T04:11:43+00:00Added an answer on June 6, 2026 at 4:11 am

    If instead of using a Poco::Thread you use a Poco::Task, you get a thread that can be cancelled. The following sample code (ready to run as-is) should give you an idea:

    #include <Poco/Task.h>
    #include <Poco/TaskManager.h>
    #include <Poco/Thread.h>
    
    #include <string>
    #include <iostream>
    using namespace std;
    
    class UdpListenerTask : public Poco::Task {
    public:
        UdpListenerTask(const string& name) : Task(name) { }
    
        void runTask() {
            cout << name() << ": starting" << endl;
            while (! isCancelled()) {
                // Do some work. Cannot block indefinitely, otherwise it
                // will never test the isCancelled() condition.
                doSomeWork();
            }
            cout << endl << name() << ": cancelled " << endl;
        }
    private:
        int doSomeWork() {
            cout << "*" << flush;
            // Simulate some time spent doing work
            int i;
            for (i = 0; i < INT32_MAX/1000; i++) { }
            return i;
        }
    };
    
    void runUdpProbe() {
        // Simulate some time spent running the probe.
        Poco::Thread::sleep(1000);
    }
    
    int main() {
        Poco::TaskManager tm;
        UdpListenerTask* st = new UdpListenerTask("task1");
        tm.start(st); // tm takes ownership
    
        // Run test 1.
        runUdpProbe();
        // Test 1 done. Cancel the UDP listener
        st->cancel();
    
        // Run all the other tests
    
        // cleanup
        tm.joinAll();
        return 0;
    }
    

    The POCO slides Multithreading give examples of usage of both Poco::Thread and Poco::Task.

    As an aside, a unit test should bypass the UDP communication via abstract classes and mock objects; I think this test should be called feature test 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've seen that a few instances of this problem have been raised already. However,
Problem: I have two arrays that can possibly be different lengths. I need to
Problem: I have a string that looks like this: [1=>2,3,4][5=>6,7,8][9=>10,11,12][13=>14,15][16=>17,18] Question: How can you
Problem : I have a hand held device that scans those graphic color barcodes
Problem I have a custom tab control using Chrome-shaped tabs that binds to a
I do have a problem working with my application. I am using AsyncTask class
I have a problem using DatagramSocket. The problem is that I can't run two
Problem: I have 2 classes, DB class and a User class, that will work
Problem : I have multiple instances of the same C# application running on different
I have this problem: have two entyties with one to one relationship, boot of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.