Skip to content

Instantly share code, notes, and snippets.

@killnine
killnine / Form1.cs
Created September 27, 2012 17:19
Asynchronous TreeView-builder Proof-Of-Concept
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace View
{
public partial class Form1 : Form
{
@killnine
killnine / SignalRConnectionComparison
Created January 26, 2013 17:38
Comparing two different SignalR implementations
// Connect to the service
var hubConnection = new HubConnection("http://localhost:8080");
// Create a proxy to the chat service
IHubProxy collectionHub = hubConnection.CreateHubProxy("CollectionHub");
IHubProxy collectionHub2 = hubConnection.CreateHubProxy("CollectionHub");
/* VERSUS */
//Connect to the service, this would be on one instance of a client
private static void FindAndConvertParameter<R, T>(IList<R> parameters, string name, out T value) where R : ApplicationParameterDto
{
value = default(T);
ApplicationParameterDto parameter = ((List<R>) parameters).Find(dto => dto.Name.Trim().ToUpper() == name.Trim().ToUpper());
if (parameter != null && !String.IsNullOrWhiteSpace(parameter.Value))
{
try
{
@killnine
killnine / ABDriver.cs
Last active December 28, 2015 19:09
Trying to use a mock NetworkStream to fake out some data that would normally come over a network device. I want to make sure my test works without being dependent on outside piece of hardware.
public class ABEthernetDriver
{
/* Some other methods and properties, constructor */
private ITcpClient _client;
private INetworkStream _nstream;
public void Connect()
{
_nstream = _client.GetStream();
byte[] numArray = new byte[] {1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 0, 5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
@killnine
killnine / NetworkStreamStub.cs
Last active December 28, 2015 21:39
Test double of a NetworkStream. Doesn't seem to modify the buffer in-place like I thought a normal NetworkStream does.
public interface IStream
{
void Write(byte[] buffer, int offset, int count);
void Close();
int EndRead(IAsyncResult asyncResult);
IAsyncResult BeginRead([In,Out] byte[] buffer, int offset, int size, AsyncCallback callback, object state);
}
public class NetworkStreamStub : IStream
{
@killnine
killnine / ABEthernetTest.cs
Created November 20, 2013 16:39
Test fixture for ABEthernet. Mostly meant to hash out a mock/fake NetworkStream. Don't want to hit actual equipment on the network in a unit test so I am trying to simulate data over the wire.
[TestFixture]
public class ABEthernetDriverTest
{
[Test]
public void ShouldConnectToABEthernetDriver()
{
//Arrange
byte[] readBuffer = new byte[] { 2, 1, 0, 0, 0, 35, 1, 2, 0, 0, 0, 0, 0, 4, 0, 5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
@killnine
killnine / TestableFactory.cs
Created December 10, 2013 18:29
Testable factory
/*
NEW STRUCTURE OF THE FACTORY
- No longer static
- Uses interface so we can mock response
*/
public interface IDriverFactory { IDriver CreateDriver(DriverType driverType, string ip); }
public class DriverFactory : IDriverFactory
{
public IDriver CreateDriver(DriverType driverType, string ip)
{
@killnine
killnine / FluentBuilderTest.cs
Last active January 4, 2016 10:18
Fluent builder
public class ProjectBuilder()
{
private int id;
private string title;
private string description;
private int teamId;
private string location;
private User user;
private Priority priority;
private Category category;
@killnine
killnine / HubConnectionManager.cs
Created July 17, 2014 17:52
The HubConnectionManager has two responsibilities: first, it exposes a more test-friendly interface by which .NET SignalR clients can connect to a server. The second is to provide a means of retrying connections to the server if `HubConnection.Start()` fails. Once a connection has been established, SignalR is more than capable at retrying connec…
using System;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.AspNet.SignalR.Client;
/* Requires Microsoft.AspNet.SignalR.Client */
/* Requires Microsoft.Bcl.Async if running on .NET 4.0 */
namespace HubConnectionManager
{
public interface IHubConnectionManager
@killnine
killnine / DialogBehavior.cs
Last active August 29, 2015 14:05
DialogBehavior for MahApps.Metro MessageBox
public class DialogBehavior : Behavior<MetroWindow>
{
public MetroWindow Window { get; set; }
protected override void OnAttached()
{
base.OnAttached();
Window = base.AssociatedObject;