Merge branch 'no-worries-i-can-wait' of github.com:ctiller/grpc into no-worries-i-can-wait

pull/2403/head
Craig Tiller 10 years ago
commit e086725e4c
  1. 12
      src/csharp/Grpc.Core.Tests/TimespecTest.cs
  2. 15
      src/csharp/Grpc.Core/Internal/Enums.cs
  3. 8
      src/csharp/Grpc.Core/Internal/Timespec.cs
  4. 8
      test/core/iomgr/tcp_client_posix_test.c

@ -61,28 +61,28 @@ namespace Grpc.Core.Internal.Tests
[Test]
public void Add()
{
var t = new Timespec { tv_sec = new IntPtr(12345), tv_nsec = new IntPtr(123456789) };
var t = new Timespec { tv_sec = new IntPtr(12345), tv_nsec = 123456789 };
var result = t.Add(TimeSpan.FromTicks(TimeSpan.TicksPerSecond * 10));
Assert.AreEqual(result.tv_sec, new IntPtr(12355));
Assert.AreEqual(result.tv_nsec, new IntPtr(123456789));
Assert.AreEqual(result.tv_nsec, 123456789);
}
[Test]
public void Add_Nanos()
{
var t = new Timespec { tv_sec = new IntPtr(12345), tv_nsec = new IntPtr(123456789) };
var t = new Timespec { tv_sec = new IntPtr(12345), tv_nsec = 123456789 };
var result = t.Add(TimeSpan.FromTicks(10));
Assert.AreEqual(result.tv_sec, new IntPtr(12345));
Assert.AreEqual(result.tv_nsec, new IntPtr(123456789 + 1000));
Assert.AreEqual(result.tv_nsec, 123456789 + 1000);
}
[Test]
public void Add_NanosOverflow()
{
var t = new Timespec { tv_sec = new IntPtr(12345), tv_nsec = new IntPtr(999999999) };
var t = new Timespec { tv_sec = new IntPtr(12345), tv_nsec = 999999999 };
var result = t.Add(TimeSpan.FromTicks(TimeSpan.TicksPerSecond * 10 + 10));
Assert.AreEqual(result.tv_sec, new IntPtr(12356));
Assert.AreEqual(result.tv_nsec, new IntPtr(999));
Assert.AreEqual(result.tv_nsec, 999);
}
}
}

@ -90,4 +90,19 @@ namespace Grpc.Core.Internal
/* operation completion */
OpComplete
}
/// <summary>
/// gpr_clock_type from grpc/support/time.h
/// </summary>
internal enum GPRClockType
{
/* Monotonic clock */
Monotonic,
/* Realtime clock */
Realtime,
/* Timespan - the distance between two time points */
Timespan
}
}

@ -55,7 +55,8 @@ namespace Grpc.Core.Internal
// NOTE: on linux 64bit sizeof(gpr_timespec) = 16, on windows 32bit sizeof(gpr_timespec) = 8
// so IntPtr seems to have the right size to work on both.
public System.IntPtr tv_sec;
public System.IntPtr tv_nsec;
public int tv_nsec;
public GPRClockType clock_type;
/// <summary>
/// Timespec a long time in the future.
@ -99,12 +100,13 @@ namespace Grpc.Core.Internal
public Timespec Add(TimeSpan timeSpan)
{
long nanos = tv_nsec.ToInt64() + (timeSpan.Ticks % TimeSpan.TicksPerSecond) * NanosPerTick;
long nanos = (long)tv_nsec + (timeSpan.Ticks % TimeSpan.TicksPerSecond) * NanosPerTick;
long overflow_sec = (nanos > NanosPerSecond) ? 1 : 0;
Timespec result;
result.tv_nsec = new IntPtr(nanos % NanosPerSecond);
result.tv_nsec = (int)(nanos % NanosPerSecond);
result.tv_sec = new IntPtr(tv_sec.ToInt64() + (timeSpan.Ticks / TimeSpan.TicksPerSecond) + overflow_sec);
result.clock_type = GPRClockType.Realtime;
return result;
}
}

@ -79,6 +79,8 @@ void test_succeeds(void) {
int r;
int connections_complete_before;
gpr_log(GPR_DEBUG, "test_succeeds");
memset(&addr, 0, sizeof(addr));
addr.sin_family = AF_INET;
@ -120,6 +122,8 @@ void test_fails(void) {
socklen_t addr_len = sizeof(addr);
int connections_complete_before;
gpr_log(GPR_DEBUG, "test_fails");
memset(&addr, 0, sizeof(addr));
addr.sin_family = AF_INET;
@ -146,13 +150,15 @@ void test_times_out(void) {
struct sockaddr_in addr;
socklen_t addr_len = sizeof(addr);
int svr_fd;
#define NUM_CLIENT_CONNECTS 10
#define NUM_CLIENT_CONNECTS 100
int client_fd[NUM_CLIENT_CONNECTS];
int i;
int r;
int connections_complete_before;
gpr_timespec connect_deadline;
gpr_log(GPR_DEBUG, "test_times_out");
memset(&addr, 0, sizeof(addr));
addr.sin_family = AF_INET;

Loading…
Cancel
Save