TIdHTTPServer raise EThread error 6 when activating it in my DUnit test
-
13-11-2019 - |
Question
I need a local HTTP server for my unit test.
When I try to activate TIdHTTPServer at SetUp
, it fails with EThread message:
"Thread Error: descriptor not valid (6)"
This is how I initialize it:
type
TestMyUnit = class(TTestCase)
...
procedure TestMyUnit.SetUp;
begin
FServer := TIdHTTPServer.Create(nil);
FServer.OnCommandGet := HTTPServerCommandGet;
FServer.Active := True; // <---- This will cause error
end;
Maybe there are some restrictions using Indy's TIdHTTPServer with DUnit framework?
- Delphi 2010
- Indy 10
Exception was thrown somewhere after this line in TIdListenerThread.Run
proc (IdCustomTCPServer unit)
LIOHandler := Server.IOHandler.Accept(Binding, Self, LYarn);
However, I can't trace it deeper, I don't know why.
Solution 2
Solved. I have my own DPK with self-designed components (used by this unit-test), and there was 1 unit with references to Indy. After I have rebuild that package, the compiler proposed me to add some new DCCReference to Indy components. After I reinstalled my package, that error in unit-test has gone.
I can't imagine the cause-and-effect relations, but now it's ok.
OTHER TIPS
You might have to allow your DUnit executable to open the port in your firewall setup... Control Panel > Windows Firewall > Advanced Settings > Inbound Rules.
Usually windows will ask you the first time the executable tries to open the port, but I would check that there is a rule in there anyway.
Edit:
Just add a bit more information, the error "Descriptor not valid" looks like a winsock error, and could be because your code can't open the port. It could be because it is blocked at the firewall (as I meantioned above) or it could be that you have another program that has that port opened... Like IIS perhaps? I would choose a port above 1024 somewhere and set your web server to use that (and obviously your client).
Edit 2:
My code for setting up my TIdHttpServer is:
FServer := TIdHttpServer.Create(nil);
FServer.DefaultPort := 7777;
FServer.AutoStartSession := True;
FServer.OnCommandGet := ServerCommandGet;
FServer.OnCreatePostStream := ServerCreatePostStream;
FServer.Active := True;