/dev/jcheng
  • About
  • Articles
  • Pages
  • Reviews
  • Tags

Articles

May 26, 2020

Gomock Tutorial

I’ve been using mock/Gomock to write tests in my personal project. When you’re building something in a new language, it is hard to prioritize learning every tool in your toolchain. For me, I’ve been writing custom and suboptimal code for Gomock because of a nifty but undocumented API call .Do. In many cases, I wan to match subsets of a complex object while ignoring irrelevant parts. e.g., verify a function is invoked with a list of User objects, but only verifying the email addresses.
May 19, 2020

Unit tests and system clock

It took me way to long to learn this. Your code (and their unit tests) should inject the system clock as a dependency. An example, let’s say you have a service that writes a record to the database with the system clock. public void save(String userName) { long currentTimeMs = System.currentTimeMillis(); User user = User.builder() .name(userName) .updateTimeMs(currentTimeMs); database.save(user); } How would you test this? You can inject a mock database instance and use it to verify that it got a User object.
May 17, 2020

Go Project Organization

Here’s a rough layout of how I organize my Go project. Some parts are situational and some parts are essential. I’ll go over both in this blog. A rough layout: + basedir +-- go.mod (module jcheng.org) +-- hello (empty) +-- log/ +-- utils/ +-- config/ +-- models/ +-- repositories/ +-- services/ +-- cmd/ +-- hello_app/ +--/cmd/ +-- speak/ +-- email/ +-- sms/ The basedir Situational. I have my (personal) projects in a monorepo.
February 27, 2020

Dependencies

Some past self version of me is saying, every class and function should be explicit about their dependencies, so that they are easily testable. John0 would say, “If you have a service that talks to a database, the database client should be an explicit dependency specified in the constructor. This makes the code easily testable.” There is another version of myself from 10 minutes ago arguing it’s foolish to be explicit about everything.
February 19, 2020

Mocking in Go Part 2

In a previous post, I talked about Gomock. I want to spend a bit more time on it. As I’ve mentioned, setting up Gomock requires Download mockgen go get github.com/golang/mock/mockgen@latest Edit your go.mod module jcheng.org go 1.13 require ( github.com/golang/mock v1.4.0 ) Add the go:generate incantation to your code //go:generate mockgen -source $GOFILE -destination mock_$GOFILE -package $GOPACKAGE package pastself import ( "time" ) ... Use mocks in your test case.
February 19, 2020

Few Annoying Things about Go

As promised, a few thoughts about Go that is annoying. No Generics Code generation is tightly coupled with the tool chain. When I need to use code generation, i.e., enums and mocks, the use case can be solved with generics instead. No lambda syntax Scala has a lambda syntax to make it easy to work with anonymous function objects val numbers = Seq(1, 5, 2, 100) val doubled = numbers.map( n => n * 2 ) Java has a similar syntax
February 14, 2020

Mocking in Go

Today I want to talk a little about the testify and gomock packages and doing mocks in Go. Mocking is essential when writing code that depends on external services, e.g., microservices, message brokers, and datastores. For many people, this means web applications: My system has business rules and talks to multiple services. How do I test the business rules without setting up all the services locally? Some people argue that mocks create a false sense of test coverage and introduce blind spots.
February 8, 2020

Why I like Go

I have a few side projects, published and unpublished. By trade, I’ve come to programming as a Java programmer. I started coding directly against the Servlet API and have coded using Enterprise Java Beans, Play Framework, Spring Framework, and even some Scala. Lately, I’ve been coding mainly in Go with some Python. Go is a really nice programming language. Some people like it for its simplicity. I want to offer a different take on why you should learn and use Go for your own projects.
February 7, 2020

Delegation

One thing about job hopping is that you get to experience new perspectives on how people work. It forces you to reconsider what’s obvious. Take delegation for example. I used to think anyone with a bit of experience can do it effectively. It’s just about decomposing a system and assigning components to different people, right? It turns out effective delegation is much more nuanced. Situational Leadership Situation Leadership is a model that ascribes different delegation styles based on the competency (skill) and commitment (motivation) of each team member.
February 3, 2020

Why Algo Interviews

Algorithm interviews are the norm in software engineering. They provide a good enough approximation on whether an engineer makes good architecture and design decisions. As example, let’s do a quick study of whether to use DynamoDB or RDBMS to store data for a project. DynamoDB Fact(s) Implemented as a distributed hashtable. Distributed by design Pros Very little operational overhead. There’s no need to create multiple DB users; No need to study query plans; No need to think about how much disk space, RAM, or CPUs to assign to your DB; Only one option for table design (flat tables).
  • ««
  • «
  • 1
  • 2
  • 3
  •  … 
  • 6
  • 7
  • 8
  • »
  • »»
© /dev/jcheng 2022