### ----------------------------------------------------
### -- MongoDB
### -- Licenses: Artistic-2.0
### -- Authors: Marcel Timmerman, Paweł Pabian
### -- File: xt/Tests/Basic/170-ServerPool.rakutest
### ----------------------------------------------------
use Test;

use BSON::Document;

use MongoDB;
use MongoDB::Client;
use MongoDB::ServerPool;
use MongoDB::ServerPool::Server;

use Base64;

#-------------------------------------------------------------------------------
my Str $log-path = @*ARGS[0];
my Version $version .= new(@*ARGS[1]);
my @server-ports = @*ARGS[2..*];
#note "args: $log-path, $version, @server-ports";

#-------------------------------------------------------------------------------
drop-send-to('mongodb');
drop-send-to('screen');
#modify-send-to( 'screen', :level(MongoDB::MdbLoglevels::Trace));
my $handle = $log-path.IO.open( :mode<wo>, :append);
add-send-to( 'mdb', :to($handle), :min-level(MongoDB::MdbLoglevels::Debug));
#set-filter(|<ObserverEmitter Timer Socket>);

info-message("Test $?FILE start");

#-------------------------------------------------------------------------------
my Str $uri = "mongodb://localhost:@server-ports[0]";
my MongoDB::Client $client .= new(:$uri);
my $uri-obj = $client.uri-obj;
my Str $host = $uri-obj.servers[0]<host>;
my Int $port = $uri-obj.servers[0]<port>.Int;

my Str $client-key = encode-base64( "s1 $host $port", :str);

my MongoDB::ServerPool $servers;

#-------------------------------------------------------------------------------
subtest "ServerPool creation", {
  dies-ok( { $servers .= new; }, '.new() not allowed');

  $servers .= instance;
  isa-ok $servers, MongoDB::ServerPool;
}

#-------------------------------------------------------------------------------
subtest "ServerPool manipulations", {
  $servers.add-server( $client-key, "$host:$port");

  my Array $topology-description = [];
  $topology-description[Topo-type] = TT-Single;
  my MongoDB::ServerPool::Server $server = $servers.select-server($client-key);

  isa-ok $server, MongoDB::ServerPool::Server;
}

info-message("Test $?FILE stop");
done-testing();


