在没有装饰器的情况下扩展类

时间:2020-10-08 21:42:57

标签: javascript typescript decorator

我想参加测试的班级

using System;
using System.Net;
using System.Net.Sockets;
using System.Text;

namespace HttpServer
{
    class Program
    {
        static void Main(string[] args)
        {
            var webserver = new Program();
            webserver.Listen(12345);
        }

        public static void ProcessTcpClientAsync(TcpClient tcpClient)
        {
            // Get a stream object for reading and writing
            NetworkStream networkStream = tcpClient.GetStream();
            byte[] buffer = readByteBufferFromNetworkStream(networkStream);
            string request = Encoding.UTF8.GetString(buffer);

            Console.WriteLine(request);

            const string HTTPNewLine = "\r\n";
            string html = "<h1>Hello from TestServer</h1>";
            string response = "HTTP/1.1 200 OK" + HTTPNewLine;
            response += "Server: TestServer 2020" + HTTPNewLine;
            response += "Content-Type: text/html; charset=utf-8" + HTTPNewLine;
            response += "Content-Length: " + html.Length + HTTPNewLine;
            response += HTTPNewLine;
            response += html;
            response += HTTPNewLine;

            byte[] responseBytes = Encoding.UTF8.GetBytes(response);
            networkStream.Write(responseBytes);

            networkStream.Close();
            tcpClient.Close();
        }

        public void Listen(Int32 port)
        {
            //Int32 port = 12345;
            IPAddress localaddr = IPAddress.Parse("127.0.0.1");

            TcpListener tcpListener = new TcpListener(localaddr, port);
            tcpListener.Start();

            // Enter the listening loop (service/daemon)
            while (true)
            {
                //@ Sync Version for debugging
                TcpClient tcpClient = tcpListener.AcceptTcpClient();
                ProcessTcpClientAsync(tcpClient);

                 //tcpListener.AcceptTcpClientAsync().ContinueWith((res) => ProcessTcpClientAsync(res.GetAwaiter().GetResult()));

            }
        }

        public static byte[] readByteBufferFromNetworkStream(NetworkStream networkStream)
        {
            byte[] buffer = new byte[256];
            int bytesRead = 0;

            while (true)
            {
                int currentBytesRead = networkStream.Read(buffer, bytesRead, buffer.Length - bytesRead);
            bytesRead += currentBytesRead;

                // If we've read less data than the size of our buffer -> it means there is no more information, so break.
                if (currentBytesRead < buffer.Length) { break; }

                // If we've reached the end of our buffer -> check to see if there's any more information, or break.
                if (bytesRead == buffer.Length)
                {
                    int nextByte = networkStream.ReadByte();

                    // End of stream? If so, we're done.
                    if (nextByte == -1)
                    {
                        break;
                    }
                    // Otherwise resize the buffer, put in the byte we've just read and continue.
                    else
                    {
                        byte[] newBuffer = new byte[buffer.Length * 2];
                        Array.Copy(buffer, newBuffer, buffer.Length);
                        newBuffer[bytesRead] = (byte)nextByte;
                        buffer = newBuffer;
                        bytesRead++;
                    }
                }
            }
            // Buffer is now too big. Shrink it.
            byte[] resultBuffer = new byte[bytesRead];
            Array.Copy(buffer, resultBuffer, bytesRead);
            return resultBuffer;
        }

    }
}

存根类

@stubOnTest('CategorizationServiceStub')
class CategorizationService {
 ...

装饰器可以“捕获”对import CategorizationService from '../../server/services/CategorizationService'; export default class CategorizationServiceStub extends CategorizationService { .... 的调用,并将其替换为模拟

CategorizationService

但是当调用export default function stubOnTest(stubbedClassStr: string) { const pathToStubbedClass = path.resolve(__dirname, stubsDir, stubbedClassStr); const stubbedClass = require(pathToStubbedClass); ... 时,它会扩展CategorizationServiceStub并调用装饰器CategorizationService

然后它变成了一个无限循环。

有办法避免这种情况吗?

0 个答案:

没有答案